5

ファイル名をデータベースに保存するアプリケーションを開発しています。Mozilla と Chrome では FileName のみが表示されますが、IE ではファイルのフル パスが表示されます。ここで、指定されたファイル名がファイル名かファイルパスかを確認したいと思います。それを行う方法はありますか?

これが私のコードです:

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

2 に答える 2

6

さて、任意のブラウザでファイル名のみを取得する場合は、次のように書く必要があります

Path.GetFileName(e.fileName);

どのブラウザでもファイル名のみが返されます ありがとう

于 2014-06-09T05:56:17.827 に答える
2

ファイルにパスがあるかどうかを確認する代わりに、 GetFileName(path);メソッドを使用するだけです

于 2014-06-09T05:58:23.147 に答える