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;
}