WWZee
WWZee

Reputation: 496

Get original filename from File

I have an action that takes in a System.File

public bool UploadToServer( File file )

And I would like to use the file's original name in the server once it gets there. I have looked in MSDN's File Class but don't see anything that looks like I could get the filename or for that matter, the filepath. Is there an attribute I can use from the File to get it's original name or should I simply make the signiture look like this:

public bool UploadToServer( File file, string fileName )

?

Solution

As @Marko suggested HttpPostedFile is what I went with, I did not have Server.Web in my resources for that project which was what was tripping me up.

Upvotes: 7

Views: 10730

Answers (1)

Marko
Marko

Reputation: 2734

Try the code below, this way you do not need to care about the path or any other security issues.

[HttpPost]
       public ActionResult Upload(HttpPostedFileBase file)
       {
           if (file != null && file.ContentLength > 0)
           {
               var fileName = Path.GetFileName(file.FileName);



               file.SaveAs(path);
           }
        }

Upvotes: 20

Related Questions