图片上传?

时间:2009-11-12 07:14:25

标签: c# model-view-controller image upload

我正在使用mvc应用程序。一个Web项目和我使用的语言是C#。

我有一个更新类别表单,并且有一个文件上传控件,请告诉我如何进行更新功能,因为在更新控制器中我们通常会传递集合对象。

请告诉我我将做什么......我将如何做。

由于 里兹

2 个答案:

答案 0 :(得分:4)

将表单元素的enctype更改为multipart form-data

<% using (Html.BeginForm(
    "upload", 
    "controller", 
    FormMethod.Post, 
    new { enctype="multipart/form-data"}
)) %>

向此表单添加文件输入:

<input type="file" name="filetoupload" id="filetoupload" />

并在控制器操作中读取文件:

public ActionResult Upload()
{
    var uploadedFile = Request.Files["filetoupload"];
    // TODO: do something with the uploaded file
    return View();
}

答案 1 :(得分:1)

控制器将具有请求属性,该属性具有文件属性。

foreach (string name in Request.Files)
{
    HttpPostedFile file = Request.Files[name];

    string filePath = Path.Combine(@"C:\Somewhere", Path.GetFileName(file.FileName));
    file.SaveAs(filePath);
}
相关问题