Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 536
WWZee Avatar asked Jul 21 '15 15:07

WWZee


People also ask

How do I find the original file name?

1 Correct answerIn the Metadata panel there is a field below File Name called Original Filename. It¨s visible when Metadata is set to Exif and IPTC. In the Metadata panel there is a field below File Name called Original Filename.

What is original filename?

The original filename of the asset resource from when it was created or imported.

How do I delete original file names?

Alternatively, head to the folder containing the files you want to delete, hit Shift + Right Click, and select Open a command window here. Then input "del [filename]" and press Enter.


1 Answers

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);
           }
        }
like image 156
Marko Avatar answered Sep 18 '22 01:09

Marko