[英]Download any kind of file in asp.net c#
我在單擊按鈕時使用此代碼,從中可以下載具有特定名稱的文件。
但是我想要,當用戶上傳文件時,其詳細信息就像任何ID證明詳細信息文件一樣。
現在,要驗證用戶管理員是否想要查看其ID證明詳細信息。 因此,他將下載用戶已上傳的文件,並將其保存在數據庫中。
因此文件可以是任何類型或擴展名。
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();
}
當用戶上傳文件時,您應該能夠使用發布文件的ContentType
屬性。 假設您使用的文件上傳控件如下:
<asp:FileUpload ID="uploadFile" runat="server" />
上傳文件后,使用
string fileType = uploadFile.PostedFile.ContentType
稍后下載該值時, Reponse.ContentType
值保存在數據庫中,並用作現有代碼中Reponse.ContentType
的值。
我想這就是你所追求的。
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();
}
因此,您可以這樣稱呼它:
string myfile = @"c:\path\To\Files\myFile.pdf"; //this wouldn't be a static string in your code
DownloadFile(myfile);
謝謝回答我的問題。 我的LinkButton可以從特定用戶ID的數據庫中下載文件,成功運行。
實際上,我的下載鏈接位於更新面板中,因此它需要“觸發器”。而且我的鏈接位於GridView中,因此我已在觸發器中傳遞了鏈接按鈕ID。
每當我們在更新面板中使用GridView時,都有一個LinkButton可以從特定用戶ID的數據庫中下載文件。 我們應該在模板字段中傳遞** GridView ID而不是LinkButton ID。**
<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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.