0

I'm trying to send binary data from a database over an ASP.NET page. I can see that the property data.Length is 2400, that means 2400 bytes, right? The weird thing is that when i receive this data the size is ~4kb, and appears to be corrupted. Any idea what the problem might be?

byte[] data = proxy.getData(id);
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=data.bin");
Response.AddHeader("Content-Length", data.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.Write(data);
Response.Flush();
2
  • Non-withstanding the answer I've posted, have you verified that data contains what you're expecting? I've written similar code before and spent ages trying to work out why the downloaded file was wrong, when it was actually the data I was sending down that was duff! =) Commented Aug 4, 2010 at 8:05
  • I've verified it by actually writing the content to a file and then redirecting to that file so that it prompts me to download it :) Not the best of solutions but it showed me that the data was valid. Anyway, thanks for the answer, it solved my problem. Commented Aug 4, 2010 at 8:14

1 Answer 1

2

There are only 4 overloads of Response.Write:

public void Write(char ch);
public void Write(object obj);
public void Write(string s);
public void Write(char[] buffer, int index, int count);

And I would suspect that, based on your code (Response.Write(data)), that you're calling the object obj overload. Have you tried:

Response.BinaryWrite(data);

This is specifically intended for writing the contents of a byte array to the output stream.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.