Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Server.MapPath to save a file

At the moment, I am saving my file to a directory hard coded in my code:

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

But I need to save my file using Server.MapPath .... Like:

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

The complete function:

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

But when I am trying to use Server.MapPath("~/App_Data/stored") it doesn't know what server is

EDIT

I can use it like: HttpContext.Current.Server.MapPath("~/App_Data/stored"); But i can't use package.Id + ".zip" with it like example: var savePath = HttpContext.Current.Server.MapPath("~/App_Data/stored"),package.Id + ".zip"));

like image 710
Java Afca Avatar asked Sep 17 '25 09:09

Java Afca


1 Answers

You can access it through the current context HttpContext.Current.Server.MapPath("~/App_Data/stored");

to get the full file path :

var filePath = Path.Combine(HttpContext.Current.Server.MapPath("~/App_Data/stored"), package.Id + ".zip");
like image 189
jekcom Avatar answered Sep 20 '25 02:09

jekcom