Click here to Skip to main content
15,909,747 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am new to this, I have created a website in which user logs in and searches a file and tries to download it. But user can download it only after giving a secret key. My question is how to download the selected file from gridview after the validation?
Posted
Comments
Modi Mayank 9-Mar-15 10:56am    
Just for other users reference, here is the complete tutorial on uploading and downloading selected file from gridview.

Hi, You can achieve this using different ways. If your grid contains uri of the file you want to download then just call below function by passing download file path as a parameter.
C#
public void DownloadFile(string filePath)
{
  Response.ClearContent();
  Response.Clear();
  Response.ContentType = "APPLICATION/OCTET-STREAM";
  String Header = "Attachment; Filename=\"" +   "FileName.pdf" + "\"";
  Response.AppendHeader("Content-Disposition", Header);
  System.IO.FileInfo Dfile = new System.IO.FileInfo(filePath);
  Response.WriteFile(Dfile.FullName);
  //Don't forget to add the following line
  Response.End();
}

I hope this will help you. :)
 
Share this answer
 
Comments
Prabhu92 9-Mar-15 5:55am    
Thanks for your reply. I am downloading the file from the database. My question is when the user selects the file from the gridview he need to download the file after giving the secret key. So how to download the file after the validation.

Thanks in advance
Prabhu92 9-Mar-15 5:58am    
"Enter secret Key option" is in the next page. So how to download the selected file in the next page. Is it possible to use Session?
Manoj K Bhoir 9-Mar-15 6:42am    
Yes you can use Session or you can validate your secrete key on same page and if validation is successful then redirect to download page. Also if you are storing your file into the database in byte() format replace Response.WriteFile(Dfile.FullName); this line with
Response.BinaryWrite(bytes); to download file where byte is the array of file contents.
Prabhu92 9-Mar-15 6:58am    
Thanks a lot
Manoj K Bhoir 9-Mar-15 7:09am    
Welcome. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900