Asp. NET download large file implementation method

Source: Internet
Author: User
Tags flush types of tables

This article mainly introduced the ASP.net download large file implementation method, more detailed analysis of the implementation of large file download ideas and specific considerations, the need for friends can refer to the

The example of this article describes the implementation of ASP.net download large files. Share to everyone for your reference. The specific analysis is as follows:

When our site needs to support downloading large files, if not control may cause users to visit the download page when there is no response, causing the browser to crash. You can avoid this problem by referring to the following code.

A few notes about this code:

1. Get the data by dividing it into smaller portions and then moving it to the output stream for downloading.

2. Specify Response.ContentType according to the type of file being downloaded. (Refer to Oschina's site to find most file types of tables: http://tool.oschina.net/commons)

3. Remember to call Response.Flush every time you finish writing Response ()

4. Using response.isclientconnected in the process of cyclic downloading can help the program find out if the connection is normal as soon as possible. If not, you can give up the download early to free up the server resources you occupy.

5. At the end of the download, you need to call Response.End () to ensure that the current thread can be terminated at the end.

The code is as follows:

Using System;

Namespace WebApplication1

{

public partial class DownloadFile:System.Web.UI.Page

{

protected void Page_Load (object sender, EventArgs e)

{

System.IO.Stream iStream = null;

Buffer to read 10K 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 = Server.MapPath ("/") + "./files/textfile1.txt";

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.Clear ();

Response.ClearHeaders ();

Response.clearcontent ();

Response.ContentType = "Text/plain"; Set the file type

Response.AddHeader ("Content-length", datatoread.tostring ());

Response.AddHeader ("Content-disposition", "attachment; Filename= "+ filename);

Read the bytes.

while (dataToRead > 0)

{

Verify That's 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;

}

}

}

catch (Exception ex)

{

Trap the error, if any.

Response.Write ("Error:" + ex.) message);

}

Finally

{

if (IStream!= null)

{

Close the file.

Istream.close ();

}

Response.End ();

}

}

}

}

I hope this article will help you with the ASP.net program design.

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.