Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 下载存储在数据库中的名称为的pdf文件_C#_Asp.net - Fatal编程技术网

C# 下载存储在数据库中的名称为的pdf文件

C# 下载存储在数据库中的名称为的pdf文件,c#,asp.net,C#,Asp.net,我正在下载一个pdf文件,我正在使用e.CommandArgument传输文件,但我想为文件添加不同的头,因为我的文件名附带了GUID,因此e.CommandArgument也有GUID,因此下载文件时附带GUID,我不希望下载文件上有GUID。那么,在我的内容处置标题中应该更改什么呢 我正在数据库中以列名receiptfilename存储没有GUID的文件名,因此,如果有人能告诉我,我应该在代码中做什么更改,以下载只附带文件名而不附带GUID的文件 这是我的aspx代码: <asp:Te

我正在下载一个pdf文件,我正在使用
e.CommandArgument
传输文件,但我想为文件添加不同的头,因为我的文件名附带了GUID,因此
e.CommandArgument
也有GUID,因此下载文件时附带GUID,我不希望下载文件上有GUID。那么,在我的内容处置标题中应该更改什么呢

我正在数据库中以列名
receiptfilename
存储没有GUID的文件名,因此,如果有人能告诉我,我应该在代码中做什么更改,以下载只附带文件名而不附带GUID的文件

这是我的aspx代码:

<asp:TemplateField HeaderText="Receipt" SortExpression="Receipt">
        <ItemTemplate>
            <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Download" CommandArgument='<%# Bind("filename") %>' Text='<%# Bind("ReceiptFileName") %>' ></asp:LinkButton> //I want to download file with this LinkButton text i.e. ReceiptFileName
        </ItemTemplate>
</asp:TemplateField>

这里有多个选项

选项1:通过由分隔字符分隔的
CommandArgument
传递多个值

<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Download" CommandArgument='<%# Eval("filename") + "," + Eval("ReceiptFileName")%>' Text='<%# Bind("ReceiptFileName") %>'></asp:LinkButton>
选项2:您可以直接访问
LinkButton1
文本属性,如下所示

protected void gridContributions_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Download")
    {
         GridViewRow row = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;
         string strFileName = ((LinkButton)row.FindControl("LinkButton1")).Text;
         ...
         ...
    }
} 

更改
content disposition
标题。是的,我知道我必须更改它,但您有什么建议我应该将其更改为?谢谢您,我感谢您的帮助。
protected void gridContributions_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Download")
    {
        string[] commandArgs = e.CommandArgument.ToString().Split(new char[] { ',' });
        string fileNameWithGUID = commandArgs[0];
        string fileNameWithoutGUID = commandArgs[1];

        Response.Clear();
        Response.ContentType = "application/pdf";
        Response.AppendHeader("content-disposition", "attachment; Filename=" + fileNameWithoutGUID + ".pdf");
        Response.TransmitFile(Server.MapPath("~/Match/Reciepts/") + fileNameWithGUID);
        Response.End();
    }
} 
protected void gridContributions_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Download")
    {
         GridViewRow row = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;
         string strFileName = ((LinkButton)row.FindControl("LinkButton1")).Text;
         ...
         ...
    }
}