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

本文关键字:Asp Net Mvc 文件 请求 | 更新日期: 2023-09-27 18:36:21

我正在尝试从我的页面上传文件,但请求没有收到发布的文件。

我的表单是正常的引导模式,这是视图。

@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参数,但我的文件没有。此外,请求文件没有任何项目。

我做错了什么??

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

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
}