Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check FileName From HttpHostedFileBase is File Name or File Path

I am developing an application which store filename in database. For Mozilla & Chrome it is showing FileName only but in IE it is showing full path of file. Now I want to check whether given filename is filename or filepath. Is there any way to do it?

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");
}
like image 625
Dhwani Avatar asked Jun 09 '14 05:06

Dhwani


1 Answers

Well,If you go with getting filename only in any browser then you should write

Path.GetFileName(e.fileName);

It will return filename only in any browser Thanks

like image 199
Just code Avatar answered Sep 24 '22 11:09

Just code