Sending a byte[] as response in webapp

  • Thread starter Thread starter Adam Clauss
  • Start date Start date
A

Adam Clauss

Alright, I have a webapp in which I need to send a byte[] (which happens to
be the contents of an excel spreadsheet read in from the .xls file).

Now, I can't actually redirect the user (Response.Redirect()) to the file,
so I want to just send the byte[] itself.

My question is, Response.Write() only takes a char[], so how can I send this
byte[]? (And what other headers might I need to set since I'm doing this
sorta manually?)

Thanks!
 
Try Response.BinaryWrite()

Visual Programming Ltd Ibex PDF Creator - High speed scalable XSL-FO mail PO
Box 22-222, Khandallah, Wellington, New Zealand site Level 2, 2 Ganges Road,
Khandallah, Wellington, New Zealand phone +64 4 479 1738 fax +64 4 479 1294
web http://www.xmlpdf.com
 
hi,

also you need to set the correct content/type, here is an example:

Response.Clear();
Response.ContentType = "application/vnd.ms-excel";
string file = Session["ReportFile"].ToString();
Response.AppendHeader( "content-disposition", "inline;filename=" +
file.Substring( file.LastIndexOf( @"\")+1) );
FileStream fileStream = new FileStream( file , FileMode.Open);
byte[] buffer = new Byte[4096];
while( fileStream.Read(buffer, 0, 4096)>0 )
Response.BinaryWrite( buffer);
fileStream.Close();
Response.End();


Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


helmut fish said:
Try Response.BinaryWrite()

Visual Programming Ltd Ibex PDF Creator - High speed scalable XSL-FO mail
PO Box 22-222, Khandallah, Wellington, New Zealand site Level 2, 2 Ganges
Road, Khandallah, Wellington, New Zealand phone +64 4 479 1738 fax +64 4
479 1294 web http://www.xmlpdf.com
Adam Clauss said:
Alright, I have a webapp in which I need to send a byte[] (which happens
to be the contents of an excel spreadsheet read in from the .xls file).

Now, I can't actually redirect the user (Response.Redirect()) to the
file, so I want to just send the byte[] itself.

My question is, Response.Write() only takes a char[], so how can I send
this byte[]? (And what other headers might I need to set since I'm doing
this sorta manually?)

Thanks!
 
Back
Top