Asp.net generates and downloads Excel files (solves the problem of using the thunder download page instead of the file)

Source: Internet
Author: User

Here, we use the method of converting an Excel file from the server and then downloading it using the file address.

First try the response. writefile method:

 Fileinfo  Fi = New  Fileinfo ( Excelfile ); // Excelfile is the address of the file on the server.  Httpresponse  Contextresponse = Httpcontext . Current . Response ; Contextresponse .Clear (); Contextresponse . Buffer = True ; Contextresponse . Charset = "Gb2312" ; // Set the type to Chinese to prevent garbled characters  Contextresponse . Appendheader ( "Content-disposition" , String . Format ("Attachment; filename = {0 }" , Excelname )); // Define the output file and file name  Contextresponse . Appendheader ( "Content-Length" , Fi . Length . Tostring ()); Contextresponse . Contentencoding = Encoding . Default ;Contextresponse . Contenttype = "Application/MS-excel" ; // Set the output file type to an Excel file.  Contextresponse . Writefile ( Fi . Fullname ); Contextresponse . Flush (); Contextresponse . End ();

The Excel file in the first line is the address of the Excel file on the server, for example, "C: \ website \ Excel \ xx.xlsx ".

This method is also commonly provided on the Internet, but in actual operations, there is a problem of inintention:

In Chrome

Everything works. the Excel file is directly downloaded to the default download folder of chrome.

In Firefox

Because the flashgot plug-in is installed, the application download tool is selected first:

The display is normal. If you select "save file", the Excel file will also be saved to the default folder. However, if you try a third-party download tool, such as thunder, the following window will appear:

 

 

Note that in the URL column, viewstate information is added after the actual address of the page, and the saved name is not the name of the Excel file, but the name of the page.

After clicking confirm, the downloaded file is converted into a local file (the file will first be changed to a. ZIP file and then to an actual file)

Under IE7

The "save" dialog box will pop up first. The file is normal. Similarly, when you click "save", the "Download" dialog box will pop up. Unlike Firefox, there is no viewstate information at the end of the URL.

 

 

 

 

 

Click OK to download the page file:

If you click Cancel in the Download Dialog Box of thunder, ie will be used for download. The file here is correct again:

It is suspected that thunder re-Requests the download Based on the URL in the download dialog box. It has nothing to do with the page initiating the request, and IE does not transmit the viewstate information to thunder, the downloaded file is not an Excel page.

Later, I tried the multipart download method, which is actually invalid, because thunder does not care about the download mechanism you provided to it, and when I call thunder in Firefox, because the viewstate of multipart download does not contain the complete information of the Excel file, the downloaded viewstate is also incomplete.

Finally, we can only use the oldest solution: Response. Redirect () to switch to the actual file address.

  fileinfo   fi  =  New   fileinfo  ( excelfile );  httpresponse   contextresponse  =  httpcontext .  current .  response ;  contextresponse .  redirect  ( string .  Format  ( "~ /Template/{0} ",  excelname ),  false ); 

In this way, the test is normal in three browsers, because the actual file address is requested, and the actual file address is displayed in thunder. Download will not cause problems. However, this is equivalent to notifying the client of the actual address of the file, which is not private.But fortunately, this does not require good privacy, and the file will be deleted after a certain period of time, so it is not a big problem.

 

The above is the result of my first consideration. It seems that I am still a little lazy ......

Afterwards, since every time thunder actually re-Requests the URL, we should input a URL for thunder to generate an Excel file.

That is, when you click the "generate EXCEL" button, go to another export page and complete the steps of generating an Excel file and downloading an Excel file in the page_load Method on this page.

 String  Filename = Request . Querystring [ "FILENAME" ]; String  Exportname =Request . Querystring [ "Export" ]; If ( Filename ! = Null ){ Exportmanger . Createexcel ( Filename ); // Create an Excel file on the server first.  Response . Redirect ( String .Format ( "{0 }? Export = {1 }" , Request . Path . Tostring (), Filename )); // Redirect to this page, but the query parameter is changed to export. } Else if ( Exportname ! = Null ){ Exportmanger . Exportexcel ( Exportname );// Download the Excel file. }

The page jumps twice. The first is to generate an Excel file and the second is to download an Excel file.

Two jumps are made because thunder captures the final URL. If the URL is generated and downloaded together, Thunder will regenerate an Excel file during the download. Download the Excel fileCodeExportmanger.Exportexcel(ExportnameThe response. Write method introduced at the beginning of this article is used. You can also use the multipart download method:

 If ( Fi . Length > 0 ){ Filestream  Sr = New  Filestream ( Fi .Fullname , System . Io . Filemode . Open , System . Io . Fileaccess . Read , System . Io . Fileshare . Read ); Int Size = 1024; // Set the length of each read operation.  For ( Int  I = 0; I < Fi . Length / Size + 1; I ++ ){ Byte [] Buffer = New byte [ Size ];Int  Length = Sr . Read ( Buffer , 0, Size ); Contextresponse . Outputstream . Write ( Buffer , 0, Length );} Sr . Close ();} Else { Contextresponse . Writefile ( Fi . Fullname );}

 

Here, only one Excel file is generated and retained on the server. You can use the "Export" parameter to download the same file each time you download the file. If you only need a one-time file, and each download needs to be regenerated, you only need to download and generate the export page. Then, the response. Write method at the beginning is changed:

 
Contextresponse.Flush();Fi.Delete();Contextresponse.End();
 
That is, after each response is cleared, the file is deleted first and then the response is ended.

In this way, the downloading tool cannot be used, the privacy of the Server File address is protected, and the multipart write method can be used to write large files, in addition, the generated files can be deleted as needed without occupying server space.

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.