Checking the file name from HttpHostedFileBase - is the file name or file path

I am developing an application that stores a file name in a database. For Mozilla and Chrome, it only shows FileName, but in IE it shows the full path to the file. Now I want to check if the given file name is filename or filepath. Is there any way to do this?

Here is my code:

public ActionResult Save(IEnumerable<HttpPostedFileBase> attachments)
{
  byte[] image = null;
  var file = attachments.First();
  // Some browsers send file names with full path. We only care about the file name.
  string filePath = Server.MapPath(General.FaxFolder + "/" + file.FileName);
  file.SaveAs(filePath);
  FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
  using (BinaryReader br = new BinaryReader(fs))
  {
    image = br.ReadBytes((int)fs.Length);
  }
  TempData["Image"] = image;
  System.IO.File.Delete(filePath);            
  return Json(new { status = "OK", imageString = Convert.ToBase64String(image) }, "text/plain");
}
+4
source share
2 answers

Well, if you want to get the file name only in any browser, then you should write

Path.GetFileName(e.fileName);

It will return the file name only in any browser. Thanks

+5
source

, , , GetFileName(path);

+2

Source: https://habr.com/ru/post/1543820/


All Articles