问题描述
我上传使用GUID因为他们的名字的文件,然后我试图下载他们,但我得到一个文件未找到错误,即使文件是存在的服务器上,我想,我做错了什么带命令实际参数的,但我不知道是什么。请告诉我,我错了,任何帮助是AP preciated。
数据库模式:
我有2列:ReceiptFileName - 存储文件名,不GUID的UI。
文件名 - 存储文件名以GUID
.aspx的code:
<&ItemTemplate中GT;
< ASP:LinkButton的ID =LinkButton1=服务器的CommandName =下载CommandArgument ='<%#绑定(文件名)%GT;'文字='<%#绑定(ReceiptFileName)%>' >< / ASP:LinkButton的>
< / ItemTemplate中>
< / ASP:的TemplateField>
code上传:
{
如果(FileUpload1.HasFile)
{
//检查文件扩展名和放大器;尺寸
字符串文件名= FileUpload1.PostedFile.FileName;
{
文件名=文件名+ Guid.NewGuid();
}
INT文件大小= FileUpload1.PostedFile.ContentLength;
如果(文件大小>(20 * 1024))
{
Label1.Text =请上传一个zip或pdf文件;
} 字符串fileextention = System.IO.Path.GetExtension(FileUpload1.FileName);
如果(fileextention.ToLower()=.ZIP&放大器;!&安培;!fileextention.ToLower()=.PDF)
{
Label1.ForeColor = System.Drawing.Color.Green;
Label1.Text =请上传一个zip或pdf文件; } 其他
{
//字符串ReceiptFileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
字符串ReceiptFileName = Path.GetFileName(文件名); //存盘 FileUpload1.SaveAs(使用Server.Mappath(Reciepts /+ ReceiptFileName));
}
code对于立即下载:
保护无效gridExpenditures_RowCommand(对象发件人,GridViewCommandEventArgs E)
{
如果(e.CommandName ==下载)
{
Response.Clear();
Response.ContentType =应用程序/八位字节流;
Response.AppendHeader(内容处置,文件名=+ e.CommandArgument);
Response.TransmitFile(使用Server.Mappath(〜/报销/ Reciepts /)+ e.CommandArgument);
到Response.End();
}
}
这可能是仅仅是愚蠢的,但我只能读取文件路径是不同的:
FileUpload1.SaveAs(使用Server.Mappath(Reciepts /+ ReceiptFileName));
VS
Response.TransmitFile(使用Server.Mappath(〜/报销/ Reciepts /)
最后一个路径具有报销
在里面。第一次没有。
I am uploading files using GUID for their names and then I am trying to download them but I am getting a file not found error even when file is there on the server,I think, I am doing something wrong with command arguement but I am not sure what. Please tell me where I am wrong, any help is appreciated.
Database schema: I have 2 columns : ReceiptFileName - Stores filename without GUID for UI. filename - Stores filename with GUID.
Aspx code:
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Download" CommandArgument='<%# Bind("filename") %>' Text='<%# Bind("ReceiptFileName") %>' ></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
Code for Upload:
{
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));
}
Code For 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();
}
}
It might be just silly but I can only read the file paths are different:
FileUpload1.SaveAs(Server.MapPath("Reciepts/" + ReceiptFileName));
Vs:
Response.TransmitFile(Server.MapPath("~/Reimbursement/Reciepts/")
The last path has Reimbursement
in it. The first doesn't.
这篇关于System.IO.FileNotFoundExecption,即使文件是present的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!