Files are saved to the database as binary streams and downloaded.

Source: Internet
Author: User
An internal email was sent and received last week, and attachments are supported ,,
In order to do a good job in this attachment, I did find a lot of information and asked a lot of people for help. Now I am posting the code for discussion and improvement, so as to facilitate those who need it,

Table Structure table name myfile: ID, files (image), type (varchar (50)

Upload
Using system;
Using system. Data;
Using system. configuration;
Using system. collections;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;

Using system. Data. sqlclient;
Using system. IO;

Public partial class image: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
}
Protected void button#click (Object sender, eventargs E)
{

Stream filedatastream = fileupload1.postedfile. inputstream;

Httppostedfile file = fileupload1.postedfile;
Int filelength = fileupload1.postedfile. contentlength;

Byte [] filedata = new byte [filelength];
Filedatastream. Read (filedata, 0, filelength); // fill the file stream in the array

String filetype = path. getextension (fileupload1.postedfile. filename); // get the file extension

Sqlconnection Cn = new sqlconnection (configurationmanager. appsettings ["dbconnectionstring"]. tostring ());
CN. open ();

Sqlcommand cmd = new sqlcommand ("insert myfile (files, type) values (@ files, @ type)", CN );
Cmd. Parameters. addwithvalue ("@ Files", filedata );
Cmd. Parameters. addwithvalue ("@ Type", filetype );
Cmd. executenonquery ();
CN. Close ();
}
}
<Head runat = "server">
<Title> upload a file to the database </title>
</Head>
<Body>
<Form ID = "form1" runat = "server" enctype = "multipart/form-Data">
<Div>
<Asp: fileupload id = "fileupload1" runat = "server"/>
<Asp: button id = "button1" runat = "server" text = "button" onclick = "button#click"/> </div>
</Form>
</Body>

Download

Using system;
Using system. Data;
Using system. configuration;
Using system. collections;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;

Using system. Data. sqlclient;
Using system. IO;

Public partial class image2: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
Sqlconnection Cn = new sqlconnection ("Server =.; uid = sa; Pwd = sa; database = test;"); // Database Link
CN. open ();

Sqldataadapter da2 = new sqldataadapter ("select * From myfile where id = 24", CN); // read 5th data records from the warehouse
Dataset ds2 = new dataset ();
Da2.fill (DS2 );
Byte [] b2 = (byte []) ds2.tables [0]. Rows [0] ["Files"];
String type = (string) ds2.tables [0]. Rows [0] ["type"];

Response. Clear ();
String type = checktype (type );
Response. addheader ("content-disposition", "attachment; filename = ABC" + type );

Response. addheader ("Content-Length", b2.length. tostring ());
Response. contenttype = type;

Response. binarywrite (B2 );
Response. End ();

String filename = (linkbutton) sender). commandargument;
Response. Clear ();
Response. contenttype = type;
Response. addheader ("content-disposition", "attachment; filename =" + httputility. urlencode (filename, system. Text. encoding. utf8 ));
Response. writefile (filename );
Response. End ();
}
Private string checktype (string filename)
{
String contenttype;
Switch (filename. substring (filename. lastindexof ("."). Trim (). tolower ())
{
Case ". ASF ":
Contenttype = "Video/X-MS-ASF ";
Break;
Case ". Avi ":
Contenttype = "Video/Avi ";
Break;
Case ". Doc ":
Contenttype = "application/MSWord"; break;
Case ". Zip ":
Contenttype = "application/zip"; break;
Case ". xls ":
Contenttype = "application/vnd. MS-excel"; break;
Case ". GIF ":
Contenttype = "image/GIF"; break;
Case ". jpg ":
Contenttype = "image/JPEG"; break;
Case "Jpeg ":
Contenttype = "image/JPEG"; break;
Case ". wav ":
Contenttype = "audio/WAV"; break;
Case ". MP3 ":
Contenttype = "audio/mpeg3"; break;
Case ". mpg ":
Contenttype = "Video/MPEG"; break;
Case ". mepg ":
Contenttype = "Video/MPEG"; break;
Case ". rtf ":
Contenttype = "application/rtf"; break;
Case ". html ":
Contenttype = "text/html"; break;
Case ". htm ":
Contenttype = "text/html"; break;
Case ". txt ":
Contenttype = "text/plain"; break;
Default:
Contenttype = "application/octet-stream ";
Break;
}
Return contenttype;
}
}
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.