views:

142

answers:

2

Okay so i have page which bacially gets a file as a Byte[] from a webservice which I then give a Save/Cancel dialogue to the user to save that file. This file can either be xml or cvs format. When the file is downloaded the file is incomplete e.g. This is what the file should have:
USD,EUR,Euro,1.2,1.1,11/15/2009,15:23:27
USD,AUD,Australian,1.25,1.15,11/15/2009,15:23:27

but when saved from the browser it only has::
USD,EUR,Euro,1.2,1.1,11/15/2009,15:23:27
USD,AUD,Australian,1.25,1.

here is my code:

Response.Buffer = true;
Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName); 
Response.AddHeader("Content-Length", MyFile.Length.ToString()); 
FileInfo file = new FileInfo(fileName); 
Response.ContentType = Utility.ReturnExtension(file.Extension.ToLower()); 
Response.BinaryWrite(MyFile); 
Response.Flush(); 
Response.Close(); 
Response.End();

I have already made sure the byte[] i am getting from webservice is okay by writing the file on my machine using following code:

StreamWriter writer = null;
string blah = System.Text.ASCIIEncoding.ASCII.GetString(MyFile);
writer = File.CreateText(Server.MapPath(filePath+fileName));
writer.Write(blah);
writer.Close();
writer.Dispose();

If I remove the Close() and End() then the whole file is rendered with some HTML code from the page. I get complete file(only XML format) if i first create/write file on my local machine and then do Response.TransmitFile(file).

I am not sure what i am doing wrong. Maybe its just something very simple. Any help will be greatly appreciated

+1  A: 

Try removing

Response.AddHeader("Content-Length", MyFile.Length.ToString());

And see if you get the full file when you do that. If you do, then you're getting a bad length from your file.

Joseph
I did try removing the "Content-Lenght" but no luck. I also tried setting the length to higher than what it was getting but still didn't work.
Dip G
A: 

You might be filling up the Response buffer. Try sending unbuffered, or if that doesn't work spread the file over several binarywrite calls and flush the buffer after each.

Joel Coehoorn
Tried sending it unbuffered but that didn't fix it. Haven't tried spreading the file over several binarywrite calls since it was such a small file.
Dip G