0

I am calling Rest API and fetching the file content stream and creating file under one of the folder application physical path. for example below is my network application physical path in stage environment.

\\Stage104\staging\Stage20003us\tcsapps\webroot\EmployerMaster\download

Img:Stage environment network path

The functionality here is whenever API calls, i am trying to deleting all the files under 'download' and creating the newly fetched file(pdf/image etc) from Rest API.

Deleting and Create File Code:

string currfile = System.AppDomain.CurrentDomain.BaseDirectory.ToString() + "download"; //System.Web.Hosting.HostingEnvironment.MapPath(string.Format("~/download/"));
System.IO.DirectoryInfo di = new DirectoryInfo(currfile);
foreach(FileInfo file in di.GetFiles()) {
    file.Delete();
}
using(var file = new FileStream(HttpContext.Current.Server.MapPath(string.Format("~/download/{0}.{1}", fileName, fileExt)), FileMode.Create)) { // create a new file to write to
 Stream contentStream = content.ReadAsStreamAsync().Result; // get the actual content stream
 contentStream.CopyTo(file); // copy that stream to the file stream
 //file.FlushAsync(); // flush back to disk before disposing
}
return string.Format("/download/{0}.{1}", fileName, fileExt);

I am getting error as below, i am not sure this error is because of deleting files or creating new file. How to overcome this issue? since the logic is working fine in my local system, i thought it will in Stage or Prod environment.

Access to the path 'NewlyCreated.pdf' is denied.
AdithyaM
  • 59
  • 1
  • 2
  • 10

0 Answers0