【问题标题】:MVC Chrome image full pathMVC Chrome 图像完整路径
【发布时间】:2017-04-23 08:03:49
【问题描述】:

我无法在 chrome 中获取图像完整路径,但在 IE 中可以使用相同的代码。每当我从另一个位置选择图像时,它都会抛出错误

“System.IO.FileNotFoundException”类型的异常发生在 mscorlib.dll 中,但未在用户代码中处理 附加信息:找不到文件“C:\Program Files\IIS Express\Chrysanthemum.jpg”。

下面是我的Controller代码

public ActionResult Create([Bind(Include = "id,Name")] Employee employee, HttpPostedFileBase file)
{
    if (ModelState.IsValid)
    {
        string Name = @"C:\temp\yourfile.png";
        System.IO.File.WriteAllBytes(Name, System.IO.File.ReadAllBytes(file.FileName));

        employee.image = System.IO.File.ReadAllBytes(file.FileName);
        db.Employees.Add(employee);
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    return View(employee);
}

下面是我的MVC代码

<input type="file" name="file" id="file" />
<input type="submit" name="submitButton" value="Save" />

由于这个问题,我无法将图像保存在任何地方。 请帮我解决问题。

【问题讨论】:

    标签: c# asp.net-mvc google-chrome file-io


    【解决方案1】:

    有/可能有几个问题。

    1. 不要使用硬编码的本地文件路径,即 C:\ 等,使用 Server.Mappath
    2. 还要检查您要访问的资源(即您的情况下的图像)是否位于您的 MVC 应用程序的相同或底层虚拟目录中
    3. 还要检查您是否具有上述资源的访问权限。

    除非您想从其他域访问资源,否则上述检查应该可以解决您的问题。

    【讨论】:

    • 嗨@Baua 感谢您的回复。实际上我没有使用硬编码路径。它发生在异常中。我正在从本地系统中选择图像并希望将其保存在项目位置,但它本身会引发错误。
    猜你喜欢
    • 1970-01-01
    • 2011-07-25
    • 2020-05-07
    • 1970-01-01
    • 1970-01-01
    • 2010-11-22
    • 1970-01-01
    • 2016-03-31
    • 2012-04-27
    相关资源
    最近更新 更多