Analysis of ASP. NET File Download Functions

Source: Internet
Author: User

What is the usage of ASP. NET File Download functions? Add the following code to your Page_Load:

 
 
  1. Page. Response. Clear ();
  2. BoolSuccess = ResponseFile (Page. Request, Page. Response,"Target file name",@"Source file path", 1024000 );
  3.  If(! Success)
  4. Response. Write ("An error occurred while downloading the file! ");
  5. Page. Response. End ();

The function code for downloading ASP. NET files is:

 
 
  1. Public Static BoolResponseFile (HttpRequest _ Request, HttpResponse _ Response, string _ fileName, string _ fullPath,Long_ Speed)
  2. {
  3. Try 
  4. {
  5. FileStream myFile =NewFileStream (_ fullPath, FileMode. Open, FileAccess. Read, FileShare. ReadWrite );
  6. BinaryReader br =NewBinaryReader (myFile );
  7. Try 
  8. {
  9. _ Response. AddHeader ("Accept-Ranges","Bytes");
  10. _ Response. Buffer =False;
  11. LongFileLength = myFile. Length;
  12. LongStartBytes = 0;
  13. DoublePack = 10240;// 10 K bytes 
  14. // Int sleep = 200; // 5 times per second, that is, 5*10 K bytes per second 
  15. IntSleep = (Int) Math. Floor (1000 * pack/_ speed) + 1;
  16. If(_ Request. Headers ["Range"]! = Null)
  17. {
  18. _ Response. StatusCode = 206;
  19. String [] range = _ Request. Headers ["Range"]. Split (New Char[] {'=','-'});
  20. StartBytes = Convert. ToInt64 (range [1]);
  21. }
  22. _ Response. AddHeader ("Content-Length", (FileLength-startBytes). ToString ());
  23. If(StartBytes! = 0)
  24. {
  25. // Response. AddHeader ("Content-Range", string. Format ("bytes {0}-{1}/{2}", startBytes, fileLength-1, fileLength )); 
  26. }
  27. _ Response. AddHeader ("Connection","Keep-Alive");
  28. _ Response. ContentType ="Application/octet-stream";
  29. _ Response. AddHeader ("Content-Disposition","Attachment; filename ="+ HttpUtility. UrlEncode (_ fileName, System. Text. Encoding. UTF8 ));
  30. Br. BaseStream. Seek (startBytes, SeekOrigin. Begin );
  31. IntMaxCount = (Int) Math. Floor (fileLength-startBytes)/pack) + 1;
  32.  
  33. For(IntI = 0; I <maxCount; I ++)
  34. {
  35. If(_ Response. IsClientConnected)
  36. {
  37. _ Response. BinaryWrite (br. ReadBytes (Int. Parse (pack. ToString ())));
  38. Thread. Sleep (sleep );
  39. }
  40. Else 
  41. {
  42. I = maxCount;
  43. }
  44. }
  45. }
  46. Catch 
  47. {
  48. Return False;
  49. }
  50. Finally
  51. {
  52. Br. Close ();
  53.  
  54. MyFile. Close ();
  55. }
  56. }
  57. Catch 
  58. {
  59. Return False;
  60. }
  61. Return True;
  62. }

In this way, the open/save window can pop up no matter what format the file is downloaded.

The basic information about ASP. NET File Download functions is introduced here. I hope to help you understand ASP. NET File Download functions.

  1. Analysis of health monitoring in ASP. NET project development
  2. Introduction to ASP. NET cache and Its Application
  3. Analysis and Practice of ASP. NET Cache
  4. Analysis of ASP. NET database cache
  5. Custom Control DateTimePicker of ASP. NET source code

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.