shindhu
shindhu

Reputation: 36

FileUpload in ASP.NET, what is the path of the uploaded file.

I have a problem in uploading a file in ASP.NET web application. I am not able to get the correct physical path of a file in hard drive.

I have tried using Server.Mappath(fileupload.postedfile.filename) and path.GetFullPath(fileupload.postedfile.filename).

Both are pointing the wrong path. How could I solve this issue?

Upvotes: 0

Views: 2331

Answers (2)

Mrunalini
Mrunalini

Reputation: 256

Try using below code .

Server.MapPath("~/Files") returns an absolute path based on a folder relative to your application

protected void UploadFile(object sender, EventArgs e)
{

    string folderPath = Server.MapPath("~/Files/");

    //Check whether Directory (Folder) exists.
    if (!Directory.Exists(folderPath))
    {
        //If Directory (Folder) does not exists. Create it.
        Directory.CreateDirectory(folderPath);
    }

    //Save the File to the Directory (Folder).
    FileUpload1.SaveAs(folderPath + Path.GetFileName(FileUpload1.FileName));

    //Display the success message.
    lblMessage.Text = Path.GetFileName(FileUpload1.FileName) + " has been uploaded.";
}

Check is performed whether the Folder (Directory) exists. If it does not then the Folder (Directory) is created.

Then the uploaded File is saved into the Folder (Directory).

Upvotes: 2

Mehrdad Tajdini
Mehrdad Tajdini

Reputation: 121

You get the uploaded file as a Stream on the server. you should save it on the disk and then use it (or use the Stream directly).

Upvotes: 0

Related Questions