Google Chrome and IE11 give an error when downloading a text file

When migrating a web server ASP.Net Web Forms applications from a single phys.users of Google Chrome and Internet Explorer 11 no longer have the ability to download files from one server to another. Each time the file appears in the download list, but it does not download, and next to the file name in the download list, there is an inscription "Error: Network error" in Chrome, and in IE11 "Failed to download myfilename.txt". Both on the old and on the new server, Windows Server 2008 R2, but IIS is slightly different in points menu. With the Mozilla FireFox browser, there are no problems with downloading the file.

To upload the file to the client, the following method was used

protected void UnloadFileToClient(string fullfilename)
{
    if (fullfilename.Length > 0)
    {
        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(fullfilename);

        try
        {
            // Open the file.
            iStream = new System.IO.FileStream(fullfilename, 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 that 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;
                }
            }
        }
        catch (Exception ex)
        {
            // Trap the error, if any.
            Response.Write("Error : " + ex.Message);
        }
        finally
        {
            if (iStream != null)
            {
                //Close the file.
                iStream.Close();
            }
            Response.Close();
        }
    }

In an attempt to solve the problem, I made the following changes

Response.Clear(); //что-то чистим
Response.ClearHeaders(); //еще что-то чистим
Response.ContentType = "text/plain"; //указываем конкретный MIME-тип
Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);

And

Response.End(); //явно указываем, что мы закончили
Response.Close();

But it doesn't help. The behavior of neither Chrome nor FireFox changes.

In the "MIME Types" settings of the ASP application in IIS -

.txt text/plain Унаследовано

Author: 4per, 2017-09-01

1 answers

It turned out that you need to add the file length to the HttmlResponse before starting the data transfer

Response.AddHeader("Content-Length", dataToRead.ToString());
 0
Author: 4per, 2017-09-02 01:14:28