Qiuzman
Qiuzman

Reputation: 1739

Uploading and saving a file to IIS virtual directory outside the root folder

Based on Microsoft's website as well as even this forum the general consensus seems to be to keep uploaded user files from the website outside of the wwwroot folder (C:\inetpub\wwwroot) and even better outside of the system drive. I have setup a virtual directory(C:\inetpub\files) in IIS for my file uploads which is outside of the wwwroot but still on the C drive (we only have one drive and I cannot partition it to make another drive). So hopefully this is still considered secure in that aspect! My issue however is I use the following code to get the directory to my hosting enviroment:

                    var filePath = Path.Combine(env.WebRootPath, document.DOCUMENT_LOCATION);
                    var fileName = document.FILENAME_AFTER_UPLOAD;
                    var fullPath = Path.Combine(filePath, fileName);

I am not sure exactly what file path I am suppose to use for saving to virtual directory. The virtual directory has an alias of "files" so its virtual path is /files so do I use env.WebRootPath + "/files" or is there some other way to access the virtual directory/path? For background document is a model object from a SQL query that returns my file path to save to and the filename we create in the SQL server.

Upvotes: 0

Views: 3605

Answers (1)

Pritom Sarkar
Pritom Sarkar

Reputation: 2252

So,you want to upload a file outside of that in the parent directory of env.webrootpath means wwwroot folder.so for that try this below code:-

var filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/img", document.Document.FileName);

above, env.webrootpath no need to use because you want to path more dynamic.

Or if you want to upload to C drive instead of wwwroot.

string SavePath = Path.Combine(Directory.GetCurrentDirectory(), (@"C:\", model.FormFile.FileName);

Upvotes: 2

Related Questions