ASP.NET MVC-无法在同一Ajax Post Call中上载信息和文件

时间:2019-04-12 15:59:14

标签: c# asp.net-mvc

我需要将信息上传到数据库中,我的控制器接收了这些参数,然后根据我的业务需求来使用它们。

当我尝试仅上载参数时,它可以工作,但是当我尝试上载参数+文件中的上载文件时,它不起作用。

我试图更改代码的构建方式,但是没有用

HTML:

<div class="box-body">
                <div class="row">
                    <div class="col-md-6">
                        <div class="form-group">
                            <label>Departamento</label>
                           @Html.DropDownList("Departamentos", (IEnumerable<SelectListItem>)ViewBag.Departamentos, "Selecione um Departamento", new { @class = "form-control select2", @id = "DDL_DEP", onchange = "FillBooks(this.value);" })

                        </div>
                        <!-- /.form-group -->

                        <!-- /.form-group -->

                        <div class="form-group">
                            <label>Tipo</label>
                            @Html.DropDownList("Tipo", (IEnumerable<SelectListItem>)ViewBag.Tipo, "Selecione um Tipo", new { @class = "form-control select2", @id = "DDL_TIPO" } )

                        </div>

                        <div class="form-group">
                            <label>Título do Ticket</label>
                            @Html.TextBox("TicketTitulo", null, new { @class = "form-control" })
                        </div>

                        <div class="form-group">
                            <label>Descrição do Ticket</label>
                            @Html.TextArea("TicketDesc", null, new { @class = "form-control", @style = "resize: Vertical; " })
                            </div>

                        <div class="form-group">

                                <label for="exampleInputFile">Anexo</label>
                                <input type="file" id="exampleInputFile">
                                <p class="help-block">Insira o seu anexo.</p>
                        </div>

                        <div class="box-footer">
                            <button type="submit" id="btn-save"  class="btn btn-primary pull-right">Guardar</button>
                            <button type="submit" class="btn btn-default">Voltar</button>

                        </div>

                        </div>
</div>

jQuery:

$(document).ready(function () {
          $("#btn-save").click(function (e) {
              var IdDep = $('#DDL_DEP').val();
              var IdTipo = $('#DDL_TIPO').val();
              var TituloTicket = $('#TicketTitulo').val();
              var DescTicket = $('#TicketDesc').val();
              var formData = new FormData();

              var file = document.getElementById("exampleInputFile").files[0];
              formData.append("exampleInputFile", file);

              $.ajax({ 
                  url: '@Url.Action("InsertTicket", "Tickets")',
                  type: "POST",
                  dataType: "JSON",
                  cache: false,
                  contentType: false,
                  processData: false,
                  data: { a: IdDep, b: IdTipo, c: TituloTicket, d: DescTicket, e: formData },
                  success: function (data) {

                  },
                  error: function (error) {
                     alert(error.responseText);
                  }
              });
          });
 })

控制器:

public ActionResult InsertTicket(int a, int b, string c, string d, HttpPostedFileBase e)
{
    // BUSINESS CODE THAT I CAN'T SHOW BUT IT DOESN'T MATTER
}

1 个答案:

答案 0 :(得分:1)

尝试一下:

$(document).ready(function () {

              $("#btn-save").click(function (e) {
                  var IdDep = $('#DDL_DEP').val();
                  var IdTipo = $('#DDL_TIPO').val();
                  var TituloTicket = $('#TicketTitulo').val();
                  var DescTicket = $('#TicketDesc').val();
                  var formData = new FormData();

                  var file = document.getElementById("exampleInputFile").files[0];
                  formData.append("e", file);
                   formData.append("a", IdDep );
                  formData.append("b", IdTipo);
                  formData.append("c", TituloTicket);
                  formData.append("d", DescTicket);

                  $.ajax({ 
                      url: '@Url.Action("InsertTicket", "Tickets")',
                      type: "POST",
                      dataType: "JSON",
                      cache: false,
                      contentType: false,
                      processData: false,
                      data:  formData ,
                      success: function (data) {

                      },
                      error: function (error) {
                         alert(error.responseText);
                      }
                  });
              });
     })