目前,我将我的文件保存到我的代码中硬编码的目录中:
var filePath = Path.Combine(@"C:\users\my documents\github\project\source\project\App_Data\stored\", package.Id + ".zip");
但是我需要使用 Server.MapPath 保存我的文件.... 喜欢:
FileInfo userFile = new FileInfo(Path.Combine(Server.MapPath("~/App_Data/stored"), package.Id));
完整的功能:
public void CompressAndDeleteSources(FlinkeMailPackage package)
{
var filePath = Path.Combine(@"C:\users\my documents\github\project\source\project\App_Data\stored\", package.Id + ".zip");
using (ZipFile zipFile = new ZipFile(filePath))
{
foreach (var file in package.FlinkeMailFileList)
{
string bestandsNaam = @"C:\users\my documents\github\project\source\project\App_Data\uploads\" + file.OriginalName;
zipFile.AddFile(bestandsNaam);
}
zipFile.Save();
}
foreach (var file in package.FlinkeMailFileList)
{
var filePathToDelete = @"C:\users\my documents\github\project\source\project\App_Data\uploads\" + file.FileName;
File.Delete(filePathToDelete);
}
}
但是当我尝试使用 Server.MapPath("~/App_Data/stored")
时,它不知道服务器是什么
编辑
我可以像这样使用它:HttpContext.Current.Server.MapPath("~/App_Data/stored");
但我不能像示例那样使用 package.Id + ".zip"
:var savePath = HttpContext.Current.Server.MapPath("~/App_Data/stored"),package .Id + ".zip"));
最佳答案
可以通过当前上下文HttpContext.Current.Server.MapPath("~/App_Data/stored");
获取完整文件路径:
var filePath = Path.Combine(HttpContext.Current.Server.MapPath("~/App_Data/stored"), package.Id + ".zip");
关于asp.net - 使用 Server.MapPath 保存文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16893487/