Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set physical path to upload a file in Asp.Net?

I want to upload a file in a physical path such as E:\Project\Folders.

I got the below code by searching in the net.

//check to make sure a file is selected
if (FileUpload1.HasFile)
{
    //create the path to save the file to
    string fileName = Path.Combine(Server.MapPath("~/Files"), FileUpload1.FileName);
    //save the file to our local path
    FileUpload1.SaveAs(fileName);
}

But in that, I want to give my physical path as I mentioned above. How to do this?

like image 745
thevan Avatar asked Oct 28 '25 02:10

thevan


1 Answers

Server.MapPath("~/Files") returns an absolute path based on a folder relative to your application. The leading ~/ tells ASP.Net to look at the root of your application.

To use a folder outside of the application:

//check to make sure a file is selected
if (FileUpload1.HasFile)
{
    //create the path to save the file to
    string fileName = Path.Combine(@"E:\Project\Folders", FileUpload1.FileName);
    //save the file to our local path
    FileUpload1.SaveAs(fileName);
}

Of course, you wouldn't hardcode the path in a production application but this should save the file using the absolute path you described.

With regards to locating the file once you have saved it (per comments):

if (FileUpload1.HasFile)
{
    string fileName = Path.Combine(@"E:\Project\Folders", FileUpload1.FileName);
    FileUpload1.SaveAs(fileName);

    FileInfo fileToDownload = new FileInfo( filename ); 

    if (fileToDownload.Exists){ 
        Process.Start(fileToDownload.FullName);
    }
    else { 
        MessageBox("File Not Saved!"); 
        return; 
    }
}
like image 51
Tim M. Avatar answered Oct 29 '25 16:10

Tim M.