The realization idea and code of asp.net large file downloading

Source: Internet
Author: User
Tags end flush net string types of tables

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.

 
 
  1. Using System;
  2. Namespace WebApplication1
  3. {
  4. public partial class DownloadFile:System.Web.UI.Page
  5. {
  6. protected void Page_Load (object sender, EventArgs e)
  7. {
  8. System.IO.Stream iStream = null;
  9. Buffer to read 10K bytes in chunk:
  10. byte[] buffer = new byte[10000];
  11. Length of the file:
  12. int length;
  13. Total bytes to read.
  14. Long dataToRead;
  15. Identify the file to download including its path.
  16. string filepath = Server.MapPath ("/") + "./files/textfile1.txt";
  17. Identify the file name.
  18. string filename = System.IO.Path.GetFileName (filepath);
  19. Try
  20. {
  21. Open the file.
  22. IStream = new System.IO.FileStream (filepath, System.IO.FileMode.Open,
  23. System.IO.FileAccess.Read, System.IO.FileShare.Read);
  24. Total bytes to read.
  25. dataToRead = Istream.length;
  26. Response.Clear ();
  27. Response.ClearHeaders ();
  28. Response.clearcontent ();
  29. Response.ContentType = "Text/plain"; Set the file type
  30. Response.AddHeader ("Content-length", datatoread.tostring ());
  31. Response.AddHeader ("Content-disposition", "attachment; Filename= "+ filename);
  32. Read the bytes.
  33. while (dataToRead > 0)
  34. {
  35. Verify That's the client is connected.
  36. if (response.isclientconnected)
  37. {
  38. Read the data in buffer.
  39. Length = istream.read (buffer, 0, 10000);
  40. Write the data to the current output stream.
  41. Response.OutputStream.Write (buffer, 0, length);
  42. Flush the data to the HTML output.
  43. Response.Flush ();
  44. Buffer = new byte[10000];
  45. dataToRead = Datatoread-length;
  46. }
  47. Else
  48. {
  49. Prevent infinite loop if user disconnects
  50. dataToRead =-1;
  51. }
  52. }
  53. }
  54. catch (Exception ex)
  55. {
  56. Trap the error, if any.
  57. Response.Write ("Error:" + ex.) message);
  58. }
  59. Finally
  60. {
  61. if (IStream!= null)
  62. {
  63. Close the file.
  64. Istream.close ();
  65. }
  66. Response.End ();
  67. }
  68. }
  69. }
  70. }

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.

Original link: http://www.codeceo.com/article/asp-net-big-file.html



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.