Asp.net implements attachment download (garbled)

Source: Internet
Author: User
Code

    Private     Void  Page_load (  Object  Sender, system. eventargs E)
{
If (Request. querystring [ " Recordid " ] ! = Null && Request. querystring [ " Recordid " ]. Tostring () ! = "" )
{
Startech. dbutility. adohelper = Startech. dbutility. adohelper. createhelper ( " News_instance " );
System. Data. datatable resourceds = Adohelper. executesqldataset ( " Select * From t_news_resource where recordid =' " + Request. querystring [ " Recordid " ]. Tostring () + " ' " ). Tables [ 0 ];
If (Resourceds. Rows. Count > 0 )
{
String Filepath = This . Mappath (resourceds. Rows [ 0 ] [ " Path " ]. Tostring ());
Byte [] Bytefile = Getfilebyte (filepath );
If (Bytefile ! = Null )
{
String Filetype = System. Io. Path. getextension (filepath );
String Filename = System. Io. Path. getfilename (filepath );
Response. Clear ();
Response. headerencoding = System. Text. encoding. default;
Response. addheader ( " Content-Disposition " , " Attachment; filename = " + Filename );
Response. addheader ( " Content-Length " , Bytefile. length. tostring ());
Response. contenttype = Checktype (filetype );
Response. binarywrite (bytefile );
Response. End ();
}
Else
{
Response. Write ( " <Font color = Red> no attachment is found! [<A href = 'javascript: window. Opener = NULL; window. Close () '> close </a>] </font> " );
}
}
}
Else
{
Response. Write ( " <Font color = Red> parameter error or insufficient permissions [<a href = 'javascript: window. Close () '> close </a>] </font> " );
}
}
// Obtain the binary stream of a file
Private Byte [] getfilebyte ( String Path)
{
Try
{
System. Io. filestream File = New System. Io. filestream (path, system. Io. filemode. Open, system. Io. fileaccess. Read );
Byte [] imgbyte = New Byte;
File. Read (imgbyte, 0 , Imgbyte. Length );
File. Close ();
Return Imgbyte;
}
Catch (Exception ex)
{
String Str = Ex. message;
Return Null ;
}
}
/// <Summary>
/// Obtains the corresponding "http mime" type of the output stream based on the file extension.
/// </Summary>
/// <Param name = "filetype"> </param>
/// <Returns> </returns>
Private String Checktype ( String Filetype)
{
String Contenttype;
Switch (Filetype. 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.