Dhwani
Dhwani

Reputation: 7626

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");
}

Upvotes: 5

Views: 1959

Answers (2)

Arijit Mukherjee
Arijit Mukherjee

Reputation: 3875

Instead of check whether the file has a path or not, what you can do is to just use GetFileName(path);method

Upvotes: 2

Just code
Just code

Reputation: 13801

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

Upvotes: 6

Related Questions