简体   繁体   English

图片上传?

[英]image upload?

i am having a mvc application.A web project and the language i am using is C#. 我有一个mvc application。一个Web项目,我使用的语言是C#。

i am having a update category form and in that there is a file upload control please tell me how will i do the update functionality because in the update controller we usually pass the collections object. 我有一个更新类别表单,并且有一个文件上传控件,请告诉我我将如何执行更新功能,因为在更新控制器中,我们通常传递collections对象。

please tell me what will I do..and How will I do. 请告诉我该怎么办..以及我将如何做。

Thanks Ritz 谢谢丽兹

Change the enctype of the form element to multipart form-data : 将form元素的enctype更改为multipart form-data

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

Add a file input to this form: 在此表单中添加文件输入:

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

and read the file in your controller action: 并在您的控制器操作中读取文件:

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

The controller will have a Request property, which has a Files property. 控制器将具有一个Request属性,该属性具有一个Files属性。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM