在Asp Mvc中上传单个输入的多个文件 [英] Upload multiple files with single input in Asp Mvc
问题描述
我需要使用一个单一的输入文件,因为我们不知道会有多少文件上传。
我的代码是这个,但我有问题
控制器代码:
{
if(ModelState.IsValid)
{
//对象保存逻辑
SaveFiles(消息,文件);
返回RedirectToAction(Index);
}
return View(message);
$ / code>
部分视图代码:
< form name =registrationaction =>
@ Html.ValidationSummary(true,,new {@class =text-danger})
< div class =form-group>
< label for =Content>内容:< / label>
< div class =fileupload>
附件:
< input id =filesname =filestype =filemultiple />
< / div>
< / div>
< button type =submitclass =btn btn-default compose_btn>发送消息< / button>
< / form>
我没有保存文件和保存对象的问题。
唯一的问题:
文件列表为空。
我为了上传文件,以包含 enctype = multipart / form-data
属性。
< ; form name =registrationaction =enctype =multipart / form-data>
或更好
@using(Html.BeginForm(actionName,controllerName,FormMethod.Post,new {enctype =multipart / form-data}))
,我强烈建议您将模型传递给视图,并使用强类型的 HtmlHelper
方法来创建您的html您的模型的属性。
I want to upload multiple files including Word Documents, Pdf and Images. I need to use a single input for files because we don't know how many files will be uploaded.
My code is this but I have problem I can't send files to server side.
Controller Code :
public ActionResult Create([Bind(Include = "Id,Content")] Message message, IEnumerable<HttpPostedFileBase> files)
{
if (ModelState.IsValid)
{
// Object save logic
SaveFiles(message,files);
return RedirectToAction("Index");
}
return View(message);
}
Part of view code :
<form name="registration" action="">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
<label for="Content" >Content:</label>
<textarea class="form-control compose_content" rows="5" id="Content" name="content"></textarea>
<div class="fileupload">
Attachment :
<input id="files" name="files" type="file" multiple />
</div>
</div>
<button type="submit" class="btn btn-default compose_btn" >Send Message</button>
</form>
I don't have problem with saving files and saving the object. only problem : files list is null.
I order to upload files, your form needs to include the enctype= multipart/form-data
attribute.
<form name="registration" action="" enctype= "multipart/form-data">
or better
@using (Html.BeginForm(actionName, controllerName, FormMethod.Post, new { enctype= "multipart/form-data" }))
and I strongly recommend you pass a model to the view and use the strongly typed HtmlHelper
methods to create your html for the properties of your model.
这篇关于在Asp Mvc中上传单个输入的多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!