2

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

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"));

4

2 回答 2

4

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

获取完整的文件路径:

var filePath = Path.Combine(HttpContext.Current.Server.MapPath("~/App_Data/stored"), package.Id + ".zip");
于 2013-06-03T09:25:51.797 回答
-1
            string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
            if (fileName != "")
            {

                FileUpload1.SaveAs(Server.MapPath("../../Files/" + fileName));
                aComplain.FileName = fileName;
                aComplain.FilePath = "../../Files/" + fileName;

                string message = aComplainManager.SaveComplain(aComplain);
                messageResult.Text = message;
                messageResult.ForeColor = Color.Green;
                Clear();
            }

            else if (fileName == "")
            {
                string message = aComplainManager.SaveComplain(aComplain);
                messageResult.Text = message;
                messageResult.ForeColor = Color.Green;
                Clear();
            }
于 2014-05-21T11:29:21.293 回答