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

前端 未结 1 1805
有刺的猬
有刺的猬 2020-12-21 11:11

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 th

相关标签:
1条回答
  • 2020-12-21 11:53

    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 ...
    }
    
    0 讨论(0)
提交回复
热议问题