Upload and download Asp.net files

Source: Internet
Author: User

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 ">

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.