我正在使用GUID为他们的名字上传文件,然后我正在尝试下载它们但是我收到文件未找到错误,即使文件在服务器上,我认为,我做错了命令争论但是我不确定是什么。请告诉我哪里错了,感谢任何帮助。
数据库架构: 我有2列:ReceiptFileName - 存储没有GUID的文件名用于UI。 filename - 使用GUID存储文件名。
Aspx代码:
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Download" CommandArgument='<%# Bind("filename") %>' Text='<%# Bind("ReceiptFileName") %>' ></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
上传代码:
{
if (FileUpload1.HasFile)
{
//check file Extension & Size
string filename = FileUpload1.PostedFile.FileName;
{
filename = filename + Guid.NewGuid();
}
int filesize = FileUpload1.PostedFile.ContentLength;
if (filesize > (20 * 1024))
{
Label1.Text = "Please upload a zip or a pdf file";
}
string fileextention = System.IO.Path.GetExtension(FileUpload1.FileName);
if (fileextention.ToLower() != ".zip" && fileextention.ToLower() != ".pdf")
{
Label1.ForeColor = System.Drawing.Color.Green;
Label1.Text = "Please upload a zip or a pdf file";
}
else
{
//string ReceiptFileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
string ReceiptFileName = Path.GetFileName(filename);
//save file to disk
FileUpload1.SaveAs(Server.MapPath("Reciepts/" + ReceiptFileName));
}
Downlaod代码:
protected void gridExpenditures_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Download")
{
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AppendHeader("content-disposition", "FileName=" + e.CommandArgument);
Response.TransmitFile(Server.MapPath("~/Reimbursement/Reciepts/") + e.CommandArgument);
Response.End();
}
}
答案 0 :(得分:0)
这可能只是愚蠢但我只能读取文件路径不同:
FileUpload1.SaveAs(Server.MapPath("Reciepts/" + ReceiptFileName));
Vs的:
Response.TransmitFile(Server.MapPath("~/Reimbursement/Reciepts/")
最后一条路径中包含Reimbursement
。第一个没有。