| |

VerySource

 Forgot password?
 Register
Search
View: 824|Reply: 5

Set Response.ContentType = "application / octet-stream"; after downloading the text file is empty bytes? Please expert

[Copy link]

1

Threads

3

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-7 17:00:01
| Show all posts |Read mode
It ’s like this, I wrote the following code, a text file on the server, now this text file does exist, and this code is local, my other 2 web servers (win2k3) are ok, only in it When a web server is downloaded, the text file is empty-there is no content, please help!

    private void DownloadTemplate (string filepath)
    {
       
        System.IO.Stream iStream = null;
        byte [] buffer = new Byte [10000];
        int length;
        long dataToRead;
        string filename = System.IO.Path.GetFileName (filepath);
        try
        {
            if (! System.IO.File.Exists (filepath))
                return;
            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 =" + HttpUtility.UrlEncode (filename, System.Text.Encoding.UTF8)); // 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
                {
                    dataToRead = -1;
                }
            }
        }
        catch (Exception ex)
        {
            showError ("", "StyleDef.ascx", "DownloadTemplate", "", "Fail: doing download for stylefile. message:" + ex.Message, "");
          
        }
        finally
        {
            if (iStream! = null)
            {// Close the file.
                iStream.Close ();
            }

            Response.Close ();
        }
    }
Reply

Use magic Report

0

Threads

322

Posts

115.00

Credits

Newbie

Rank: 1

Credits
115.00

 China

Post time: 2020-1-7 21:27:01
| Show all posts
Debug it,

If you can't debug, add debug statements.

Although you say the file definitely exists.

Still check here.
if (! System.IO.File.Exists (filepath))
 {

      Response.Write ("File does not exist!");
               return;

}
Reply

Use magic Report

1

Threads

3

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

 Author| Post time: 2020-1-7 21:45:01
| Show all posts
It is added that this text file does have content, two of which can be downloaded and displayed normally! Only one server does not work. I checked the settings of IIS, and it is the same. Is there any special setting in metabase.xml or machine.config?
Reply

Use magic Report

1

Threads

3

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

 Author| Post time: 2020-1-7 22:36:01
| Show all posts
I have debugged locally, it's completely normal, it only works on the remote web server, and it doesn't work on some web services. The program runs without error prompts, just no content, which is strange!
Reply

Use magic Report

0

Threads

322

Posts

115.00

Credits

Newbie

Rank: 1

Credits
115.00

 China

Post time: 2020-1-8 00:09:01
| Show all posts
So add some debugging information.

Add points everywhere you have branches

As above, still

dataToRead is> 0
Reply

Use magic Report

1

Threads

60

Posts

37.00

Credits

Newbie

Rank: 1

Credits
37.00

 China

Post time: 2020-1-9 17:54:01
| Show all posts
See if the file acquisition path is correct
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list