簡體   English   中英

請求未收到文件-Asp.Net Mvc

[英]Request is not receiving file - Asp.Net Mvc

我正在嘗試從我的頁面上載文件,但是請求未收到發布的文件。

我的表單進入普通的Bootstrap模式,這就是視圖。

@model InventarioWeb.Mvc.ViewModels.InventarioViewModel

<!-- Modal -->
<div id="ImportadorModal" class="modal fade" role="dialog">
    <div class="modal-dialog">

        <!-- Modal content-->
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title">Importar Arquivo</h4>
            </div>
            <div class="modal-body">
                @using (Html.BeginForm("ImportarItensInventario", "Inventario", FormMethod.Post, new { enctype = "multipart/form-data" }))
                {
                    <div class="row">
                        <div class="col-md-10">
                            @*<h4>Input Groups</h4>*@
                            <div class="input-group">
                                <span class="input-group-btn">
                                    <span class="btn btn-primary btn-file">
                                        Procurar&hellip; 
                                        <input type="file" 
                                               id="fileToUpload"
                                               accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet">
                                    </span>
                                </span>
                                <input type="text" class="form-control" readonly>
                            </div>
                            <span class="help-block">
                                Selecione um arquivo
                            </span>
                        </div>
                        <div class="col-md-10">
                            <input type="submit" id="SubmitArquivoInventario" name="Submit" value="Salvar Arquivo" class="btn btn-primary" disabled="disabled"/>
                        </div>
                    </div>
                    @*@Html.HiddenFor(x => x.InventarioId)*@
                }
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-success" data-dismiss="modal">Close</button>
            </div>
        </div>

    </div>
</div>

這是控制器的方法

[HttpPost]
        public ActionResult ImportarItensInventario(Guid inventarioId)
        {
            if (Request.Files["UploadInventarioItems"] == null || Request.Files["UploadInventarioItems"].ContentLength == 0)
            {
                return RedirectToAction(nameof(Details), new { id = inventarioId });
            }

            string path = $"{Server.MapPath("~/Uploads")}/{Request.Files["UploadInventarioItems"].FileName}";

            if (System.IO.File.Exists(path))
            {
                System.IO.File.Delete(path);
            }

            Request.Files["UploadInventarioItems"].SaveAs(path);

            var model = new InventarioViewModel {InventarioId = inventarioId};

            var result = _arquivoAppService.ImportarArquivo(InventarioViewModel.ToModel(model), Request.Files["UploadInventarioItems"].FileName);

            return RedirectToAction(nameof(Details), new { id = inventarioId});
        }

當我請求時,接收到id參數,但沒有收到。 此外,Request.Files沒有任何項目。

我做錯了什么?

name屬性添加到您的輸入類型文件中,您可以解決不使用此屬性的文件的問題,但這更加方便。

 <input type="file" id="fileToUpload" name="upload" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet">

並在服務器中使用此方法獲取文件:

if (Request.Files["upload"] == null || Request.Files["upload"].HasFile())
{
//do something
}

或對多個文件這樣:

foreach (string inputTagName in Request.Files)
    {
      if (!Request.Files[inputTagName ].HasFile()) continue;
       //... continue processing
}

暫無
暫無

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

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