【问题标题】:FileNotFoundException while uploading a image上传图片时出现 FileNotFoundException
【发布时间】:2019-11-14 19:54:30
【问题描述】:

我的目标是用户可以从他们的桌面上的任何地方选择一张图片并将其上传/发送到 Twitter。我正在努力设置 imagePath 变量的相对路径。

路径(D 盘)是我存储的图像。在运行应用程序时,它看起来在不同的路径中。这会引发错误 System.IO.FileNotFoundException。我也尝试过 Server.MapPath。帮我解决。

解决了如下问题

字符串文件路径;字符串图像路径 = "";

        if (imgFile == null)
        {
            imgFile = Request.Files["imgFile"];
        }

        if (imgFile.FileName != "")
        {
            filePath = Server.MapPath("~/Images/");
            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }
            filePath = filePath + Path.GetFileName(imgFile.FileName);
            imgFile.SaveAs(filePath);
            imagePath = Path.Combine(Server.MapPath(@"~/Images/"), filePath);
        }

【问题讨论】:

  • 首先您需要将文件上传到您的项目目录,然后您才能阅读stream
  • imagePath变量中显示的路径是服务器路径,不是用户路径,先将文件保存在服务器位置,然后开始读取文件。
  • @MuhammadAftab ..感谢您的建议
  • @LPGTE.SOFTS 谢谢。
  • 您能否解释一下为什么将我的问题评为-1。这只是一个问题。不是吗?

标签: asp.net asp.net-mvc asp.net-mvc-5


【解决方案1】:

文件必须保存在服务器位置,尝试替换以下指令:

string imagePath=Path.Combine(Request.MapPath("~/"),fileName);

按照下面的说明

var fileName = Path.GetFileName(file.FileName);  
var imagePath = Path.Combine(Request.MapPath("~/"), fileName);
file.SaveAs(imagePath); 
........

真诚的

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-16
    • 1970-01-01
    • 1970-01-01
    • 2015-07-26
    • 1970-01-01
    • 2012-09-15
    • 1970-01-01
    相关资源
    最近更新 更多