Alexandre
Alexandre

Reputation: 13308

Asp.net mvc - get full file name of uploaded file

Is it possible to get full file name of uploaded file in asp.net mvc?

UPDATE The data contains only the file name, but doesn't the file path! See the attached image for details.

screenshot

Upvotes: 2

Views: 8626

Answers (1)

Scott Rippey
Scott Rippey

Reputation: 15810

It depends on the browser.
Most browsers (FF, Chrome, Safari) do not send this information, primarily for security reasons. However, it appears as though some versions of IE do send the full client path.
This value will be stored in the FileName property of the HttpPostedFile.

The documentation for FileName should help. It says:

FileName: The name of the client's file, including the directory path.

In the following code, postedFile.FileName will vary based on the browser. Therefore, it's important to always extract just the filename, and you might also get lucky and get the clientPath too.

public ActionResult UploadFile(HttpPostedFile postedFile) {
    var clientPath = IO.Path.GetDirectoryName(postedFile.FileName);
    var filename = IO.Path.GetFileName(postedFile.FileName);
    ... Save the file, etc ...
}

Upvotes: 9

Related Questions