从HttpHostedFileBase检查FileName是文件名或文件路径

 经来泓 发布于 2023-01-10 12:56

我正在开发一个在数据库中存储文件名的应用程序 对于Mozilla和Chrome,它仅显示FileName,但在IE中显示文件的完整路径.现在我想检查给定的文件名是文件名还是文件路径.有什么办法吗?

这是我的代码:

public ActionResult Save(IEnumerable 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");
}

Just code.. 6

好吧,如果你只在任何浏览器中获取文件名,那么你应该写

Path.GetFileName(e.fileName);

它只会在任何浏览器中返回文件名谢谢

1 个回答
  • 好吧,如果你只在任何浏览器中获取文件名,那么你应该写

    Path.GetFileName(e.fileName);
    

    它只会在任何浏览器中返回文件名谢谢

    2023-01-10 13:00 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有