0

最近我对图像处理产生了兴趣。但我被困在漫长旅程的开始。

我在 asp.net mvc3(razor view) 项目中上传图像时遇到问题。任何人都可以建议我一个基本的示例/教程如何做到这一点。

4

1 回答 1

0

要上传图像,只需在您的 html 中调用它。

@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
  <input type="file" name="file" />
  <input type="submit" value="Upload" />

}

请务必拥有enctype = "multipart/form-data",否则您的文件将无法上传。然后从控制器直接处理 Request.Files 接受一个 HttpPostedFileBase

[HttpPost]
public ActionResult Index(HttpPostedFileBase file)
{
    if (file != null && file.ContentLength > 0) 
    {
        //file handling logic
        file.SaveAs(/* your path here */);
    }
    return RedirectToAction("Index");        
}

另请记住,经典文件上传不适用于 ajax 调用。如果是这种情况,您必须使用像这样的插件

于 2012-08-12T14:49:22.327 回答