Download excel from database without path

Posted by Oswaldlily under ASP.NET on 5/9/2013 | Points: 10 | Views : 1024 | Status : [Member] | Replies : 3
I have converted excelfile to byte[].
now to download excel from db,following code used..
but excel file not getting popup...
code running without errors...nothin happening..why

byte[] bytevalue = (byte[])cmd.ExecuteScalar();
excelPath = System.Text.Encoding.ASCII.GetString(bytevalue);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
Response.Clear();
Response.Buffer = true;
Response.ContentType = excelPath;
// to open file prompt Box open or Save file
Response.AddHeader("content-disposition", "attachment;filename=" + excelPath);
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite((byte[])bytevalue);

HttpContext.Current.ApplicationInstance.CompleteRequest();
//Response.End();
}




Responses

Posted by: Learningtorise on: 5/10/2013 [Member] Starter | Points: 25

Up
0
Down
You have mentioned Content-Type wrong!

Try:

Response.Clear();

Response.ClearHeaders();

Response.ClearContent();

Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);

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

Response.ContentType = "application/vnd.ms-excel"; //mime-type for .xls extension

Response.Flush();

Response.TransmitFile(file.FullName);

Response.End();


http://hashtagakash.wordpress.com/

Oswaldlily, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Oswaldlily on: 5/10/2013 [Member] Starter | Points: 25

Up
0
Down
@Learningtorise
file.Name => excel name with extension (or) full path of file

Oswaldlily, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Learningtorise on: 5/10/2013 [Member] Starter | Points: 25

Up
0
Down
Excel Name with Extension...

If Response.Transmit doesnt work Use:

Response.OutputStream.Write(ByteArray, 0, FileLength);


http://hashtagakash.wordpress.com/

Oswaldlily, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response