使用 Server.MapPath 保存文件

Jav*_*fca 4 asp.net file-upload

目前,我将文件保存到代码中硬编码的目录中:

var filePath = Path.Combine(@"C:\users\my documents\github\project\source\project\App_Data\stored\", package.Id + ".zip");
Run Code Online (Sandbox Code Playgroud)

但我需要使用 Server.MapPath .... 保存我的文件,例如:

FileInfo userFile = new FileInfo(Path.Combine(Server.MapPath("~/App_Data/stored"), package.Id));
Run Code Online (Sandbox Code Playgroud)

完整功能:

 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);
     }       
   }
Run Code Online (Sandbox Code Playgroud)

但是当我尝试使用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"));

jek*_*com 5

您可以通过当前上下文访问它 HttpContext.Current.Server.MapPath("~/App_Data/stored");

获取完整文件路径:

var filePath = Path.Combine(HttpContext.Current.Server.MapPath("~/App_Data/stored"), package.Id + ".zip");
Run Code Online (Sandbox Code Playgroud)