我有一张图片上传表格
<% 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参数有什么问题,我该如何解决?
感谢您的关注,并提前感谢您的帮助。