簡體   English   中英

使用單個輸入 Asp Mvc 上傳多個文件

[英]Upload multiple files with single input Asp Mvc

我想上傳多個文件,包括 Word 文檔、Pdf 和圖像。 我需要對文件使用單個輸入,因為我們不知道將上傳多少文件。

我的代碼是這樣的,但是我遇到了無法將文件發送到服務器端的問題。

控制器代碼:

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);
}

部分視圖代碼:

<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>

我在保存文件和保存對象方面沒有問題。 唯一的問題:文件列表為空。

我為了上傳文件,你的表單需要包含enctype= multipart/form-data屬性。

<form name="registration"  action="" enctype= "multipart/form-data">

或更好

@using (Html.BeginForm(actionName, controllerName, FormMethod.Post, new { enctype= "multipart/form-data" }))

我強烈建議您將模型傳遞給視圖,並使用強類型HtmlHelper方法為模型的屬性創建 html。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM