4

I tried to write a text file using OutputStream.Write but, I've noticed that the last character of the file is not being sent.

Whether the file is 6KB or 242KB, the last character is skipped.

AwAIB0UfFlJTSTNABEQWGgMVAhNFBkVeKgVTSx4LCVQMBUUQRUMwBVFTGwcAEAASRRNTBipNQQMFDREYB
BUAH0VIKgVLRVccCRFFGRcbR08wREgDEQENEUkCDRNOTX5cS1ZXHgQGHFYIB0NOfgUIAwMABFQABBcdUg
YpRFcDHgZBAA0TRTEDBj1KQEZXREEdRRIKHFQGK0tARgUbFRULEkUFSF9+R1FXVxwJEUUkAAFQSTBWQQ0
xBBQHDV5MUkFIOgV2RgQYDhoWE0sxTEktQAwKVx8AB0UCDRcAQyxXS1FZSBUcBAIWUldDN1dAAxEHE1QI
E0UTTkJ+TARAFgYVVBAYARdSVSpESkdXAAAcBFg=

Note: whole text is one line in the text file.

My text file is somewhat similar to the above. So can anyone help me out?

My code:

var path = Request.QueryString["path"];
path = Server.MapPath(Url.Content("~/Files/" + path + "/somefile.txt"));
Response.Buffer = false;
Response.ContentType = "text/plain";
FileInfo file = new FileInfo(path);
int len = (int)file.Length, bytes;
Response.AppendHeader("content-length", len.ToString());
byte[] buffer = new byte[1024];
Stream outStream = Response.OutputStream;
using (Stream stream = System.IO.File.OpenRead(path))
{
    while (len > 0 && (bytes = stream.Read(buffer, 0, buffer.Length)) > 0)
    {
        outStream.Write(buffer, 0, bytes);
        len -= bytes;
    }
}
Response.Flush();
Response.Close();

UPDATE 1: Now shows the complete text from the file.

UPDATE 2 : Updated the complete C# code.

UPDATE 3: Thanks, my friends, for all your efforts! I somehow made it work - the problem was Response.Flush() and Response.Close(); once I removed these 2 statements it started working. I don't understand why this problem occurred as I always use Response.Flush() and Response.Close(). I never received this kind of error but this was the first time. If anyone could give me an explanation, it would be appreciated. I will mark @AlexeiLevenkov's post as the answer, as I just tested it again without the Response.Flush and Close() and it is working.

Timo
  • 263
  • 3
  • 8
Syed Abdul Qadeer
  • 455
  • 1
  • 6
  • 14
  • Works fine for me. Perhaps there's something 'iffy' about your file? – Simon Whitehead Feb 03 '13 at 06:16
  • would you please check it once again with the above text ?? thanks – Syed Abdul Qadeer Feb 03 '13 at 07:28
  • The stream processing itself appears to be ok, so I suspect something in the way that you are handling the web request/response. A trace of the http requests/responses (header and body) might be helpful. You can capture those with a tool such as fiddler (http://www.fiddler2.com/fiddler2/) – Nathan Feb 03 '13 at 07:47
  • It might also be helpful to post any code that executes after your while loop, particularly if it deals with ``Response``. – Nathan Feb 03 '13 at 07:54
  • @Nathan : i have updated the C# code, thats all i have in the method. – Syed Abdul Qadeer Feb 03 '13 at 09:25

2 Answers2

2

Stream.CopyTo is easier approaach (as long as you can use .Net 4.0).

using (var stream = System.IO.File.OpenRead(path))
{
  stream.CopyTo(outStream);
}
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • my friend , i still have the problem .. i have edited my post , and now i am showing my whole text in the file. please test with that – Syed Abdul Qadeer Feb 03 '13 at 07:26
  • @Alexei Levenkov : if i use the your code , then i am not getting the proper output , it is not even sending the the complete file , it just sends some 3/4 of the file thats it. – Syed Abdul Qadeer Feb 03 '13 at 07:30
0

I think you need to call Response.Flush() or outStream.Flush()

Nathan
  • 10,593
  • 10
  • 63
  • 87