0

I am trying to download a password encrypted zip file that gets created on button push using dotnetzip.

On button push, I hit my report controller and call this function

[HttpGet]
[OutputCache(Duration = 60, VaryByParam = "none", Location = 
OutputCacheLocation.Client, NoStore = true)]
public FileResult AppleUserDataReport()
{
    using (ZipFile zip = new ZipFile())
    {
        zip.Password = Membership.GeneratePassword(12, 1);
        var sb = _service.GetAppleUsersByClientId(User.ClientId);
        var reportName = "AppleUserData_Downloaded_" + DateTime.UtcNow.ToString("yyyy_MM_dd");

        var contentType = "text/xml";
        var bytes = Encoding.UTF32.GetBytes(sb.ToString());

        var result = new FileContentResult(bytes, contentType);
        result.FileDownloadName = reportName + ".csv";

        zip.AddFile(result.FileDownloadName, "file");
        zip.Save("C:\\MyZipFile.zip");

        return result;
    }
}

I keep getting the error

Access to the path 'C:\DotNetZip-gy22lp3l.tmp' is denied.

I have no idea how this .tmp file is getting created or why its giving me an access denied error.

I used the same code as found in other questions with this error but kept getting the same error, any help would be great.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Carpathea
  • 97
  • 9
  • 1
    Because you are saving it to the C:\ root, your application doesn't have file permissions to put it there. Try setting the save path relative to the root of the application. eg `zip.Save(AppDomain.CurrentDomain.BaseDirectory + "MyZipFile.zip");` Alternatively, create a temp folder in your web application an point it there. – Steve Feb 21 '18 at 16:35
  • DotNetZip is trying to zip your file and it's creating a temp file in the process. As @Steve mentioned, don't write to the root folder. Use a relative path instead. – JuanR Feb 21 '18 at 16:39
  • This solves my initial problem and results in a System.IO.FileNotFoundException: C:\Program Files (x86)\IIS Express\AppleUserData_Downloaded_2018_02_21.csv exception i am investigating now thanks for your help – Carpathea Feb 21 '18 at 16:40
  • 1
    I would use Server.MapPath() or something similar. The AppDomain.CurrentDomain.BaseDirectory was just an example before I saw it was a web application. You really need to create some sort of temp folder to store these kind of files in. Alternatively use a memory stream and you wont have to touch the disk . see https://stackoverflow.com/questions/2266204/creating-zip-file-from-stream-and-downloading-it – Steve Feb 21 '18 at 16:42
  • Ideally i want to save the zipped file to the users desktop or downloads folder, this is an admin portal downloading a password protected zip folder is all. – Carpathea Feb 21 '18 at 16:48
  • 1
    Where they save it is up to them. You can't set that server side, – Steve Feb 21 '18 at 16:49
  • Ok ill create a temp directory and save it there. – Carpathea Feb 21 '18 at 16:51
  • If you're wanting to save a file, then use `Path.GetTempFileName()`, optionally create a directory and give your application permissions to write to that directory – ColinM Feb 21 '18 at 18:16

0 Answers0