0

I have an application that allows the user to upload a file (saving it to in a folder located in the wwwroot of the ASPNETCORE application). From here they can make edits to it and then they can choose to export the file as a csv/ xml/ xlsx which downloads the file to the user's 'downloads' folder.

While debugging in Visual Studio this all works fine however when I publish and deploy the application to IIS I am getting the exception

Error saving file C:\windows\system32\config\systemprofile\Downloads(FILE NAME)

Could not find part of the path C:\windows\system32\config\systemprofile\Downloads(FILE NAME)

This is the current way I am getting the downloads folder:

FileInfo file = new FileInfo(Path.Combine(Environment.ExpandEnvironmentVariables(@"%USERPROFILE%\Downloads"), data.Filename + "." + data.FileType));

However I have also tried the solution that Hans Passant has answered to a similar question here. Both solutions worjk fine while debugging locally however as soon as I publish them, this one produces the exception:

Value cannot be null. Parameter name: path1

Which I presume is thrown at this point here when I try and save the file to the user's download folder.

using (var package = new ExcelPackage(file))
{
    var workSheet = package.Workbook.Worksheets.Add("ExportSheet");
    workSheet.Cells.LoadFromCollection(exports, true);
    package.Save();
}

I don't really know how I would be able to reproduce these exceptions seeing as locally using Visual Studio it all works fine.

Has anyone else came across this issue while trying to download a file?

UPDATE: When the application is running on IIS, it seems to be using that as the user profile instead of the actually user, so when it tries to navigate to the Downloads folder, it cannot find it. How can I force it to use the user's profile?

LoadUserProfile is already set to True.

Community
  • 1
  • 1
JamesS
  • 2,167
  • 1
  • 11
  • 29
  • Maybe there is trouble with rights? – Mateech Apr 17 '20 at 10:55
  • @Mateech I wouldn't think so. The Downloads folder has read and write privileges to nearly everyone – JamesS Apr 17 '20 at 11:01
  • Learn the differences first https://blog.lextudio.com/web-application-differences-in-visual-studio-and-iis-60fec7e311b3 – Lex Li Apr 17 '20 at 12:02
  • You need a stack trace to find the exact line for the exception. You could also do some detailled debugging by confirming the output of each variable and writing it to a log file. – jscarle Apr 17 '20 at 12:35

1 Answers1

1

Web applications have no knowledge of the end-user's computer's filesystem!

So using Environment.GetFolderPath or Environment.ExpandEnvironmentVariables in server side code will only reveal the server-side user (i.e. the Windows Service Identity)'s profile directories which is completely separate and distinct from your web-application's actual browser-based users OS user profile.

As a simple thought-experiment: consider a user running a weird alien web-browser on an even more alien operating system (say, iBrowse for the Amiga!) - the concept of a Windows-shell "Downloads" directory just doesn't exist, and yet here they are, browsing your website. What do you expect your code would do in this situation?

To "download" a file to a user, your server-side web-application should serve the raw bytes of the generated file (e.g. using HttpResponse.TransmitFile) with the Content-Disposition: header to provide a hint to the user's browser that they should save the file rather than try to open it in the browser.

Dai
  • 141,631
  • 28
  • 261
  • 374