【问题标题】:ASP.Net MVC image upload failing in Google Chrome谷歌浏览器中的 ASP.Net MVC 图像上传失败
【发布时间】:2010-02-10 09:06:09
【问题描述】:

我有一个图片上传表单

<% using (Html.BeginForm("PictureValidateAndSave", "UserGallery", new {}, FormMethod.Post, new { enctype = "multipart/form-data"})) { %>
    <table>
        <tr>
            <td> Album Name: </td>
            <td> <%= Html.DropDownList("albumList") %></td>
        </tr>
        <tr>
            <td> File Location: </td> 
            <td> <input type="file" name="picture" accept="image/gif, image/jpeg" /> </td>  
        <tr>
        <tr>
            <td> Picture name: </td>
            <td> <input name="pictureName" style="width: 147px;"/> </td>
        </tr>
    </table>            
    <p> <input type="submit" value="Save" /> </p>
<% } %>

回帖到动作

public ActionResult PictureValidateAndSave(long albumList, HttpPostedFileBase picture, string pictureName)

该代码适用于除 Google Chrome 以外的所有浏览器。我的 IDE 是 Visual Studio 2k8,我还没有弄清楚如何使用它在 Google Chrome 上进行调试,但是,我抛出了错误消息,而且我知道由于某种原因在 chrome 下,以下检查没有通过:

string mimeType = picture.ContentType;

// Check for the correct mimeType to define the extension
switch (mimeType)
{
    case "image/pjpeg":
        mimeType = ".jpeg";
        break;
    case "image/png":
        mimeType = ".png";
        break;
    case "image/x-png":
        mimeType = ".png";
        break;
    case "image/gif":
        mimeType = ".gif";
        // Conversion to image
        Image gifImage = Image.FromStream(picture.InputStream);
        FrameDimension dimension = new FrameDimension(gifImage.FrameDimensionsList[0]);

        int frameCount = gifImage.GetFrameCount(dimension);
        // Reject if its an animated gif
        if (frameCount > 1)
        {
            return RedirectToAction("UploadPicture", new { error = 3 });
        }
        break;
    default:
        return RedirectToAction("UploadPicture", new { error = 1 });
}

显然,在 Chrome 下,HttpPostedFileBase 参数图片未正确编码并丢失其 mime 类型,但是,这可能不是唯一的问题。 Chrome下的HttpPostedFileBase参数有什么问题,我该如何解决?

感谢您的关注,并提前感谢您的帮助。

【问题讨论】:

  • 在 crome 中打开您的站点并对其进行调试,以查看为内容类型分配的值。而且,仅仅检查扩展并摆脱那个 switch 语句不是更容易吗?
  • 我同意 switch 语句的评论。这是调试代码。我添加了很多不需要的东西。此外,在 VS2k8 调试器运行时在 chrome 中打开站点,不会报告任何内容。就好像代码没有运行一样。
  • 如果您在 Chrome 中查看,您将能够单步执行代码。 VS 调试器附加到 VS 开发服务器,而不是浏览器实例 - 这就是它与浏览器无关的方式。我经常在 ie、ff 和 chrome 中查看我的代码。

标签: asp.net-mvc google-chrome httppostedfilebase


【解决方案1】:

只需使用 hanselmans method,在 chrome 中完美运行。

【讨论】:

  • 谢谢你,Al,查看示例帮助我确认我做的是正确的事情。这是一个我尚未确定的 mimetype 的小问题。我仍然需要阅读更多关于 jpeg 和 pjpeg 之间差异的信息,以完全理解为什么 Chrome 的做法与 IE8 和 FF 不同。 Chrome 将所有 pjpeg 识别为常规 jpeg。
【解决方案2】:

我刚刚调试了一些将文件上传到 mvc 的代码,我从 Firefox 中的 HttpPostedFileBase 获得了确切的 MIME 字符串,就像我在 Chrome 中所做的那样:分别为“image/jpeg”和“image/gif”。所以我认为浏览器解释文件类型没有问题。

看看this answer。它有很多关于上传和输入“HttpPostedFileBase”数据的代码。它可能会让你摆脱困境。

【讨论】:

  • 谢谢你。这么晚才回复很抱歉。我搁置了这个错误,直到现在我才能回到它。我发现了我的问题。显然 IE8 和 FF 解释为 image/pjpeg 在 Chrome 中被解释为 image/jpeg,这仍然有效,但我还不明白为什么会有这样的差异。
  • 我明白了。看来您可能正在上传渐进式 jpeg,而 IE 想要以不同的方式对待它们。我只是建议使用正则表达式或类似的方法来简化对 MIME 字符串变化的过滤。更多关于 IE 中渐进式 jpeg 上传的信息:stackoverflow.com/questions/115705/…
猜你喜欢
  • 1970-01-01
  • 2014-01-26
  • 1970-01-01
  • 1970-01-01
  • 2023-02-10
  • 1970-01-01
  • 2014-03-30
  • 2010-10-05
  • 2016-07-08
相关资源
最近更新 更多