Last post Mar 07, 2018 08:26 AM by AngelinaJolie
Mar 06, 2018 01:53 AM|sidd-mehta|LINK
Hi, I have an MVC application, wherein I have to save the files uploaded through a file explorer to a shared path or any other file location, apart from project files.
I have checked for examples online, but usually the approach given is using Server.MapPath logic, which does the job but it creates a folder inside the project folder itself.
So is there any System.IO functionalities available for MVC C# wherein we could be able to save files on a given location and not inside the project files or folders. Also for this will we require ASP.NET Impersonation etc., to make sure only a single user
or person has rights to access this folder, or I have to create a folder and make sure that 'everyone' has read-write access.
Also how to download the files while saving at that physical location. I tried to download a file using 'FileResult' as return type passing the file name, the byte size and the mime type (depending on the file path), but nothing happens.
I tried using IFrame on Ajax success, used window.location to redirect to same FileResult Action but nothing happens. Any way to download a file not located in the project folder?.
Do we have any examples or references or approach for the said?.
Thanks In Advance!!!..
Mar 06, 2018 01:59 AM|bruce (sqlwork.com)|LINK
Mar 06, 2018 05:44 AM|DA924|LINK
You can save the file to any location, like a network or local file share, as long as the ASP.NET worker process account that is servicing the Application Pool that is hosting the ASP.NET Web application has permission on the resource.
Mar 07, 2018 08:26 AM|AngelinaJolie|LINK
e could be able to save files on a given location and not inside the project files or folders.
Any way to download a file not located in the project folder?.
Sir, In MVC, we still could use System.IO.File beside Server.MapPath
The following example shows how to copy files and directories.
// Simple synchronous file copy operations with no user interface.
// To run this sample, first create the following directories and files:
public class SimpleFileCopy
static void Main()
string fileName = "test.txt";
string sourcePath = @"C:\Users\Public\TestFolder";
string targetPath = @"C:\Users\Public\TestFolder\SubDir";
// Use Path class to manipulate file and directory paths.
string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName);
// To copy a folder's contents to a new location:
// Create a new target folder, if necessary.
// To copy a file to another location and
// overwrite the destination file if it already exists.
System.IO.File.Copy(sourceFile, destFile, true);
// To copy all the files in one directory to another directory.
// Get the files in the source folder. (To recursively iterate through
// all subfolders under the current directory, see
// "How to: Iterate Through a Directory Tree.")
// Note: Check for target path was performed previously
// in this code example.
string files = System.IO.Directory.GetFiles(sourcePath);
// Copy the files and overwrite destination files if they already exist.
foreach (string s in files)
// Use static Path methods to extract only the file name from the path.
fileName = System.IO.Path.GetFileName(s);
destFile = System.IO.Path.Combine(targetPath, fileName);
System.IO.File.Copy(s, destFile, true);
Console.WriteLine("Source path does not exist!");
// Keep console window open in debug mode.
Console.WriteLine("Press any key to exit.");
The following example shows how to move files and directories.
// Simple synchronous file move operations with no user interface.
public class SimpleFileMove
static void Main()
string sourceFile = @"C:\Users\Public\public\test.txt";
string destinationFile = @"C:\Users\Public\private\test.txt";
// To move a file or folder to a new location:
// To move an entire directory. To programmatically modify or combine
// path strings, use the System.IO.Path class.
More infromation https://msdn.microsoft.com/en-us/library/system.io.file(v=vs.110).aspx and
With regards, Angelina Jolie