AbdulAziz
AbdulAziz

Reputation: 6278

SaveAs() file in Application's folder from fileUpload in ASP C#

I have a FileUpload control in ASP.NET.

C# Code

if (FileUpldFile.HasFile)
{
   try
   {
      //saving the file
      FileUpldLicenceFileMOT.SaveAs("c:\\SaveDirectory" + FileUpldFile.FileName);

      //Getting the Path of saved file
      var pathMot = @"c:\\SaveDirectory" + FileUpldFile.FileName;

      //Getting File Extension
      var fileExtension = Path.GetExtension(pathMot).Substring(1);
      ViewState["fileExtension_MOT"] = fileExtension;
     .....

Here I want to save the file in my application's folder, my application folder's structure is like this:

--> Root Folder
   --> Documents
      --> sampleFileFromFileUpload.png

Question: how can I save the file using SaveAs() in the application's internal folder (i.e: Root Folder > Documents) from a FileUpload component?

Upvotes: 0

Views: 2403

Answers (1)

mason
mason

Reputation: 32694

 if (FileUpldFile.HasFile)
            {
            string savelocation=Server.MapPath("~/Documents/");
                try
                {
                    //saving the file
                    FileUpldLicenceFileMOT.SaveAs(savelocation + FileUpldFile.FileName);
    ...

Use Server.MapPath() to get the absolute path by passing in a path relative to the site root. Make sure that your server has the correct permissions to access the folder you're saving to.

Upvotes: 2

Related Questions