Reputation: 39
I am using this code on button click from which I can download a file with a specific name.
But I want, when the user has uploaded a file, in his details like any Id proof details file.
Now to verify the user admin want to see the his Id proof details. So he will download the file which user had uploaded and that is saved in database.
So the file can be in any type or extension.
private void Button1_click(object sender, System.EventArgs e)
{
string filename="C:\myuploads\invoice.pdf";
Response.ContentType = "Application/pdf";
Response.AppendHeader("Content-Disposition", "attachment;" + filename +);
Response.TransmitFile(Server.MapPath(filename));
Response.End();
}
Upvotes: 1
Views: 9860
Reputation: 39
Thanks for answering my questions. My LinkButton to downloading the file from database on particular user Id worked Successfully.
Actually My Download Link was in update panel so It needs the "Trigger" for this.. And My Link was in GridView So I had Passed the Link Button Id in Trigger.
Whenever we use GridView in Update Panel and There is a LinkButton to Download the File from database on particular user Id. We Should Pass **GridView Id not the LinkButton Id in Template Field.**
<asp:UpdatePanel ID="upd" runat="server">
<ContentTemplate>
<asp:GridView ID="grd_UserList" runat="server" CssClass="table"
DataKeyNames="Uid" AutoGenerateColumns="false" AllowPaging="false">
<asp:TemplateField HeaderText="Task Name">
<ItemTemplate>
<asp:LinkButton Id="LinkDownload" runat="Server" CommandArgument='<%# Eval("Attachment") %>' >
</ItemTemplate>
</asp:TemplateField>
</asp:GridView>
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="grd_UserList" />
</Triggers>
</asp:UpdatePanel>
Upvotes: 1
Reputation: 2111
I think this is what you're after.
private void DownloadFile(string file)
{
var fi = new FileInfo(file);
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename="+ fi.Name);
Response.WriteFile(file);
Response.End();
}
So you just call it like this:
string myfile = @"c:\path\To\Files\myFile.pdf"; //this wouldn't be a static string in your code
DownloadFile(myfile);
Upvotes: 2
Reputation: 61784
When the user uploads the file, you should be able to use the ContentType
property of the posted file. This is assuming you used a file upload control like:
<asp:FileUpload ID="uploadFile" runat="server" />
When the file is uploaded, get the content type using
string fileType = uploadFile.PostedFile.ContentType
Save that value in your DB, and use it as the value of Reponse.ContentType
in your existing code, when you download it later.
Upvotes: 0