Function Code sorting for asp.net File Download

Source: Internet
Author: User

Copy codeThe Code is as follows:
Public void FileDownLoadDel (string fullFilename)
{
System. IO. Stream iStream = null;
// Buffer to read 10 K bytes in chunk:
Byte [] buffer = new Byte [10000];
// Length of the file:
Int length;
// Total bytes to read:
Long dataToRead;
// Identify the file to download including its path.
String filepath = fullFilename;
Filepath = Server. MapPath (filepath );
// Identify the file name.
String filename = System. IO. Path. GetFileName (filepath );

Try
{
// Open the file.
IStream = new System. IO. FileStream (filepath, System. IO. FileMode. Open,
System. IO. FileAccess. Read, System. IO. FileShare. Read );

// Total bytes to read:
DataToRead = iStream. Length;
Response. ContentType = "application/octet-stream ";
Response. AddHeader ("Content-Disposition", "attachment; filename =" + filename );
// Read the bytes.
While (dataToRead> 0)
{
// Verify that the client is connected.
If (Response. IsClientConnected)
{
// Read the data in buffer.
Length = iStream. Read (buffer, 0, 10000 );
// Write the data to the current output stream.
Response. OutputStream. Write (buffer, 0, length );
// Flush the data to the HTML output.
Response. Flush ();
Buffer = new Byte [10000];
DataToRead = dataToRead-length;
}
Else
{
// Prevent infinite loop if user disconnects
DataToRead =-1;
Response. Clear ();
}
}
Response. End (); // The content refreshed on the page is appended to the file.
}
Catch (Exception ex)
{
// Trap the error, if any.
// Response. Write ("Error:" + ex. Message );
// Base. WriteLog ("", ":" + ex. Message + "! ", LogType. Error, this. GetType (). ToString ());
}
Finally
{
If (iStream! = Null)
{
// Close the file.
IStream. Close ();
}
File. Delete (fullFilename );
}
}

Related Article

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.