What is the usage of ASP. NET File Download functions? Add the following code to your Page_Load:
- Page. Response. Clear ();
- BoolSuccess = ResponseFile (Page. Request, Page. Response,"Target file name",@"Source file path", 1024000 );
- If(! Success)
- Response. Write ("An error occurred while downloading the file! ");
- Page. Response. End ();
The function code for downloading ASP. NET files is:
- Public Static BoolResponseFile (HttpRequest _ Request, HttpResponse _ Response, string _ fileName, string _ fullPath,Long_ Speed)
- {
- Try
- {
- FileStream myFile =NewFileStream (_ fullPath, FileMode. Open, FileAccess. Read, FileShare. ReadWrite );
- BinaryReader br =NewBinaryReader (myFile );
- Try
- {
- _ Response. AddHeader ("Accept-Ranges","Bytes");
- _ Response. Buffer =False;
- LongFileLength = myFile. Length;
- LongStartBytes = 0;
- DoublePack = 10240;// 10 K bytes
- // Int sleep = 200; // 5 times per second, that is, 5*10 K bytes per second
- IntSleep = (Int) Math. Floor (1000 * pack/_ speed) + 1;
- If(_ Request. Headers ["Range"]! = Null)
- {
- _ Response. StatusCode = 206;
- String [] range = _ Request. Headers ["Range"]. Split (New Char[] {'=','-'});
- StartBytes = Convert. ToInt64 (range [1]);
- }
- _ Response. AddHeader ("Content-Length", (FileLength-startBytes). ToString ());
- If(StartBytes! = 0)
- {
- // Response. AddHeader ("Content-Range", string. Format ("bytes {0}-{1}/{2}", startBytes, fileLength-1, fileLength ));
- }
- _ Response. AddHeader ("Connection","Keep-Alive");
- _ Response. ContentType ="Application/octet-stream";
- _ Response. AddHeader ("Content-Disposition","Attachment; filename ="+ HttpUtility. UrlEncode (_ fileName, System. Text. Encoding. UTF8 ));
- Br. BaseStream. Seek (startBytes, SeekOrigin. Begin );
- IntMaxCount = (Int) Math. Floor (fileLength-startBytes)/pack) + 1;
-
- For(IntI = 0; I <maxCount; I ++)
- {
- If(_ Response. IsClientConnected)
- {
- _ Response. BinaryWrite (br. ReadBytes (Int. Parse (pack. ToString ())));
- Thread. Sleep (sleep );
- }
- Else
- {
- I = maxCount;
- }
- }
- }
- Catch
- {
- Return False;
- }
- Finally
- {
- Br. Close ();
-
- MyFile. Close ();
- }
- }
- Catch
- {
- Return False;
- }
- Return True;
- }
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.
- Analysis of health monitoring in ASP. NET project development
- Introduction to ASP. NET cache and Its Application
- Analysis and Practice of ASP. NET Cache
- Analysis of ASP. NET database cache
- Custom Control DateTimePicker of ASP. NET source code