请求没有收到文件 - Asp.Net Mvc

时间:2016-02-25 12:58:41

标签: c# asp.net-mvc razor

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

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

                protected void ExportExcel(object sender, EventArgs e)
                {
                    DataTable dt = new DataTable("GridView_Data");
                    foreach(TableCell cell in GridView1.HeaderRow.Cells)
                    {
                        dt.Columns.Add(cell.Text);
                    }
                    foreach (GridViewRow row in GridView1.Rows)
                    {
                        dt.Rows.Add();
                        for (int i=0; i<row.Cells.Count; i++)
                        {
                            dt.Rows[dt.Rows.Count - 1][i] = row.Cells[i].Text;
                        }
                   }
                    using (XLWorkbook wb = new XLWorkbook())
                    {
                        wb.Worksheets.Add(dt);

                        Response.Clear();
                        Response.Buffer = true;
                        Response.Charset = "";
                        Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                        Response.AddHeader("content-disposition", "attachment;filename=GridView.xlsx");
                        using (MemoryStream MyMemoryStream = new MemoryStream())
                        {
                            wb.SaveAs(MyMemoryStream);
                            MyMemoryStream.WriteTo(Response.OutputStream);
                            Response.Flush();
                            Response.End();
                        }
                    }
                }

这是控制器的方法

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

当我请求时,收到id参数,但我的文件不是。 此外,Request.Files没有任何项目。

我做错了什么?

1 个答案:

答案 0 :(得分:1)

名称属性添加到输入类型文件中,您可以解决方法以获取没有此属性的文件,但它更方便。

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