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

2020-02-14 10:23发布

问题:

Is it possible to get full file name of uploaded file in asp.net mvc?![enter image description here]

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

回答1:

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 ...
}