【发布时间】:2015-10-29 06:19:28
【问题描述】:
当用户上传多个文档时,我将他们的文件存储在我的项目中,如下所示:
Guid id;
id = Guid.NewGuid();
string filePath = Path.Combine(HttpContext.Server.MapPath("../Uploads"),
Path.GetFileName(id + item.FileName));
item.SaveAs(filePath);
所以文件在我的项目中是这样保存的:
- 1250a2d5-cd40-4bcc-a979-9d6f2cd62b9fLog.txt
- bdb31966-e3c4-4344-b02c-305c0eb0fa0aLogging.txt
现在,在创建 zip 文件时,我在提取 zip 文件时得到了与此文件相同的名称,但在用户下载文件后,我不想在我的文件名中使用 guid。
但是,我尝试从文件名中删除 guid,但收到错误 System.IO.FileNotFoundException。
这是我的代码:
using (var zip = new ZipFile())
{
var str = new string[] { "1250a2d5-cd40-4bcc-a979-9d6f2cd62b9fLog.txt", "bdb31966-e3c4-4344-b02c-305c0eb0fa0aLogging.txt" }; //file name are Log.txt and Logging.txt
string[] str1 = str .Split(',');
foreach (var item in str1)
{
string filePath = Server.MapPath("~/Uploads/" + item.Substring(36));//as guid are of 36 digits
zip.AddFile(filePath, "files");
}
zip.Save(memoryStream);//Getting error here
}
【问题讨论】:
-
filePath 定义的文件是否存在于服务器映射文件夹中?
-
@user1672994:见 Kevin 答案。他说得有道理
标签: c# asp.net-mvc zip dotnetzip