In our actual application project development, file upload and download are essential and can be said to be everywhere. File Upload under DOTNET is a simple task.. NET provides a powerful class library system. Io to implement all Io operations. It is also very simple to download files. Next I will summarize some of my practical experience and hope to help beginners.
The first is File Upload. to upload a file, you must ensure that ASP. NET users have the write permission on the server folder you want to operate on, so that the rest is very easy. For example:CodeAs follows:
/**/ /// <Summary>
///File Upload
/// </Summary>
/// <Param name = "FILENAME">Upload File Path</Param>
Private Bool SaveFile (filename)
{
String Filepath = Request. physicalapplicationpath + " \\ " + Filename;
Try
{
This. Excelfilepath. postedfile. saveas (filepath );
Return True;
}
Catch (Unauthorizedaccessexception ex)
{
Response. Write ( " <SCRIPT> alert (\ " No write permission. Access failed! Details: " + Ex. tostring () + " \ " ); </SCRIPT> " );
Return False ;
}
Catch (Exception ex)
{
Response. Write ( " <SCRIPT> alert (\ " File Upload Failed! Details: " + Ex. tostring () + " Suggestion: the file may be too large! \ " ); </SCRIPT> " );
Return False ;
}
}
I have summarized the two methods for downloading files. The sample code is as follows:
Method 1:
/**/ /// <Summary>
///File Download
/// </Summary>
/// <Param name = "FILENAME">Physical path of the downloaded file</Param>
Public Static Void Downloadfile ( String Path)
{
System. Web. httpcontext contxt = System. Web. httpcontext. Current;
If (Path ! = "" )
{
// String Path = httpcontext. Current. server. mappath (filename );
System. Io. fileinfo File = New System. Io. fileinfo (PATH );
If (File. exists)
{
Contxt. response. Clear ();
Contxt. response. addheader ( " Content-Disposition " , " Attachment; filename = " + Contxt. server. urlencode (file. Name ));
Contxt. response. addheader ( " Content-Length " , File. length. tostring ());
Contxt. response. contentencoding = System. Text. encoding. default;
Contxt. response. charset = " Gb2312 " ;
Contxt. response. contenttype = " Application/octet-stream " ;
Contxt. response. Filter. Close ();
Contxt. response. writefile (file. fullname );
Contxt. response. End ();
}
Else
{
Contxt. response. Write ("This file does not exist.");
}
}
}
Method 2:
/**/ /// <Summary>
///File Download
/// </Summary>
/// <Param name = "filepath"> </param>
Public Static Void Downloadfilestream ( String Filepath)
{
System. Web. httpcontext contxt = System. Web. httpcontext. Current;
If ( ! File. exists (filepath ))
{
Throw NewException ("This file does not exist!");
}
// Read files
Filestream downfiles = File. openread (filepath );
Byte [] Filecont = New Byte [Downfiles. Length];
Downfiles. Read (filecont, 0 ,( Int ) Downfiles. Length );
Contxt. response. Clear ();
Contxt. response. addheader ( " Content-Disposition " , " Attachment; filename = " + Contxt. server. urlencode (downfiles. Name ));
Contxt. response. addheader ( " Content-Length " , Downfiles. length. tostring ());
Contxt. response. contentencoding = System. Text. encoding. default;
Contxt. response. charset = " Gb2312 " ;
Contxt. response. binarywrite (filecont );
Contxt. response. End ();
}
Padding-left: 3px; padding-Right: 5px; "class =" noprint ">