0

I have uploaded my file and inserted its data into database, now when i click I want to download it.

bytes = (byte[])dsResult.Tables[0].Rows[0]["data"];
contentType.Text = dsResult.Tables[0].Rows[0]["contentType"].ToString();
fileName.Text = dsResult.Tables[0].Rows[0]["name"].ToString();

try
{ 
   Response.Clear();
   Response.Buffer = true;
   Response.Charset = "";
   Response.Cache.SetCacheability(HttpCacheability.NoCache);
   Response.ContentType = contentType.Text;
   Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName.Text);
   Response.BinaryWrite(bytes);                
   Response.Flush();             
}
catch (Exception ex)
{
   throw;
}

It's not throwing any error but it is not downloading file.

Jon P
  • 19,442
  • 8
  • 49
  • 72
Mohammad Shahbaz
  • 403
  • 8
  • 28

2 Answers2

0

The file download will only begin if you set the HTML header accordingly at Page_Load. Meaning: you can't render any HTML DOM elements to the user because that would be 'html/textwhereas you requirepdf/documentorexcel/document` , etc as the content-type.

So to make it easier, have the file transfer code be in a new page with Page_Load event handling the file download code. Then from the page that user was originally at, let the user click on a link button or something that redirect him/her to the download page.

What is amazing is that the file transfer will happen without the user actually having to visit another page. The file will be downloaded, and the user will still be at the source page, even though you redirected to the download page

Ahmad
  • 12,336
  • 6
  • 48
  • 88
  • its not even going into catch block – Mohammad Shahbaz Jan 15 '18 at 06:26
  • 2
    You know it is bad practice to rethrow a exception unchanged? Only `throw` is actualy better in this case. [See](https://stackoverflow.com/questions/178456/what-is-the-proper-way-to-re-throw-an-exception-in-c) – Christian Gollhardt Jan 15 '18 at 06:31
  • @MohammadShahbaz I have updated my answer. See my elaboration – Ahmad Jan 15 '18 at 06:34
  • content type value is coming from database for that file – Mohammad Shahbaz Jan 15 '18 at 06:58
  • @MohammadShahbaz yes, but you need to set up the headers before you render anything to the user. That is why you should do it early at `Page_Load` or even before. – Ahmad Jan 15 '18 at 06:59
  • I have a grid , and in that I have button for each file, when i click on button above code runs – Mohammad Shahbaz Jan 15 '18 at 07:03
  • @MohammadShahbaz in that case, you need to handle button click event for each button in the grid so that it redirects the response to the download page, but store that database values in session variables so that you can pick them up from the other download page. – Ahmad Jan 15 '18 at 07:29
0

Downloading file from DB?

You have to save your file from DB into your Project Directory Folder.

You can put this in your button event:

 string s = "\\Image\\file1.jpg";
 Response.Clear();
 Response.ClearHeaders();
 Response.ClearContent();
 Response.ContentType = "application/octet-stream";
 Response.AddHeader("Content-Disposition", "attachment; filename=" + s);
 Response.TransmitFile(Server.MapPath("~" + s));
 Response.End();
 Response.Redirect("RedirectMe.aspx", false);
Vijunav Vastivch
  • 4,153
  • 1
  • 16
  • 30