Reputation: 1773
I'm trying to send binary data to a client using Response.OutputStream but seem to be having problems with it. My code is fairly simple and pretty much identical to working code I've used before, but nothing happens on the client when the code runs.
Response.Buffer = false;
Response.ContentType = @"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AppendHeader("content-length", genstream.Length.ToString());
Response.AppendHeader("content-disposition", string.Format("attachment; filename={0}.xlsx", filename));
byte[] buffer = new byte[1024];
genstream.Position = 0;
int n;
while ((n = genstream.Read(buffer, 0, 1024) ) > 0)
{
Response.OutputStream.Write(buffer, 0, n);
}
If there code there is fine, which it seems to be, what else could be causing this behaviour?
Upvotes: 1
Views: 3882
Reputation: 24832
Use the Response BinaryWrite function and the Flush command. E.g:
Response.ContentType = @"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.BinaryWrite(buffer);
Response.Flush();
Upvotes: 2