【发布时间】: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