White screen solution for larger PDF file output to Client

Source: Internet
Author: User
Tags config file size flush memory usage

Our site to provide PDF resources to users to download, some of which have large PDF files, operating for a period of time the IIS process CPU occupancy rate, memory usage increased, the client performance is output screen. After you restart IIS, you can normally provide services externally.

Reason:

When a file download request is performed by the ASP.net worker process (aspnet_wp.exe, for applications running on Internet Information Services 6.0 [IIS), W3wp.exe) to the Microsoft Internet information Services process (Ineti Nfo.exe or Dllhost.exe) to send data.

Depending on the configuration of your computer, the IIS process may process the data, or it may cache the data in memory. If the file is too large, the data will be cached in memory as the two processes communicate with each other. This can result in increased memory usage on the server. This error occurs because of memory limitations on the WEB server.

Workaround:

Workaround 1:

Large files are cut into small chunks of data and then incrementally added to the output stream, the code sample given on MSDN

Code

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 = "DownloadFileName";
    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.ContentType = "Application/octet-stream"; Response.AddHeader ("Content-disposition", "attachment;
        Filename= "+ filename);
         Read the bytes.
            while (dataToRead > 0) {//Verify, 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;
        (Exception ex) {//Trap the error, if any. Response.Write ("Error:" + ex.)
    message);
            finally {if (IStream!= null) {//close the file.
        Istream.close (); }     }

Workaround 2:

Change the <compilation debug= "true" Batch= "false" > configuration section in the Web.config file of the site to: <compilation debug= "false" Batch= "false" >

Explanation on MSDN:

When you set the Debug property value of a compilation element to False in the ASP.net application's Web.config file, you must set the Server.ScriptTimeout property to the appropriate value for the file size that you want to download. By default, the Server.ScriptTimeout value is set to 90 seconds. However, when the Debug property is set to True, the Server.ScriptTimeout value is set to a very large value (30,000,000 seconds). As a developer, you must know how this can affect the behavior of your asp.net Web application.

Because the development environment will default to Web.config this configuration section as a debug state when we build Web applications, this will degrade the performance of Web applications, so we often ignore this configuration when deploying.

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.