我正在尝试上传图像,使用 HttpPostedFileBase.InputStream.Read() 方法将上传流读取到缓冲区,但它给了我一个转换错误
file.InputStream.Read(imageSize, 0, file.ContentLength);
请在下面找到代码,我错过了什么
@using (Html.BeginForm("Create", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<fieldset>
<legend>Upload Image</legend>
@Html.Label("Title")
@Html.Editor("fileTitle")<br />
Upload File: <input type="file" name="test" />
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
[HttpPost]
public ActionResult Create(string fileTitle)
{
try
{
HttpPostedFileBase file = Request.Files[0];
byte[] imageSize = new byte[file.ContentLength];
**file.InputStream.Read(imageSize, 0, file.ContentLength);**
Image image = new Image()
{
Name = file.FileName.Split('\\').Last(),
Size = file.ContentLength,
Title = fileTitle,
ID = 1,
Image1 = imageSize
};
db.Images.AddObject(image);
db.SaveChanges();
return RedirectToAction("Detail");
}
catch(Exception e)
{
ModelState.AddModelError("uploadError", e);
}
return View();
}