Get original filename from file

I have an action that takes System.File

public bool UploadToServer( File file )

      

And I would like to use the original filename on the server as soon as it gets there. I have looked at MSDN File Class , but I don't see anything that looks like I can get the file name or, most importantly, the file path. Is there an attribute that I can use from File

to get its original name, or just make a link like this:

public bool UploadToServer( File file, string fileName )

      

?

Decision

Since @Marko suggested that HttpPostedFile

is what I went with, I didn't have Server.Web

in my resources for this project, which was what was turning me off.

+3


source to share


1 answer


Try using the below code so you don't have to worry about the path or any other security issue.



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



               file.SaveAs(path);
           }
        }

      

+11


source







All Articles