AJAX error when sending data with files

0

Hello, I am trying to upload a file from exel to a database / SQL SERVER 2008 through AJAX . But I have a problem with the file when I send the data ... at this moment I have not managed to mount the file in the query to execute it, on the contrary the error that ajax sends is that of the file, I can not send it myself. I would greatly appreciate your help :)

I do not have any errors in the console or in the browser more than this, I suppose maybe I should try the file or something to upload it.

<form id="uploadForm">
  <div class="container">
    <div class=row "">
      <div class="col-lg-6">
        <table class="table table-hover">
          <thead>
            <tr>
              <th>Firstname</th>
              <th>Lastname</th>

            </tr>
          </thead>
          <tbody>
            <tr>
              <td>Nombre</td>
              <td>
                <input type="text" placeholder="NombreDeProyecto" id="name" />
              </td>

            </tr>
            <tr>
              <td>Formato</td>
              <td>
                <label>Formato: </label> <input type="file" id="format" />
              </td>

            </tr>
            <tr>
              <td>Wiw</td>
              <td>
                <p>
                  @Session["Wiw"]
                  <input type="text" class="hidden" id="wiw" value="@Session[" WiW "]" />
                </p>
              </td>

            </tr>
            <tr>
              <td>
                <input type="button" onclick="tryx()" name="name" value="trymeplz" />
              </td>
            </tr>
          </tbody>
        </table>
      </div>
      <div class="col-lg-6"></div>
    </div>
</form>
</div>



<script>
  function tryx() {
    var form = new FormData($("#uploadForm")[0]);
    $.ajax({
      url: '@Url.Action("KyoTest", "Home")',
      method: "POST",
      dataType: 'json',
      data: form,
      processData: false,
      contentType: false,
      success: function(result) {
        alert("at least Ajax looks fine" + response);
      },
      error: function(er) {
        alert("something got wrong" + er);
      }
    });

  }
</script>

[HttpPost]
public ActionResult KyoTest(testsoru form) {

  string constr = "Data Source=DMX87025;Initial Catalog=DB_PCC;Integrated Security=True";



  using(SqlConnection con = new SqlConnection(constr)) {
    string query = "INSERT INTO Testsoru([Name],[Wiw],[Formato],[Size],[FName],[Type])" +
      " VALUES(@Name,@wiw,@format,@Size,@FName,@Type)";

    using(SqlCommand cmd = new SqlCommand(query, con)) {
      cmd.Parameters.AddWithValue("@Name", form.name);
      cmd.Parameters.AddWithValue("@wiw", form.wiw);
      cmd.Parameters.AddWithValue("@format", form.formato);
      cmd.Parameters.AddWithValue("@Size", DBNull.Value);
      cmd.Parameters.AddWithValue("@FName", DBNull.Value);
      cmd.Parameters.AddWithValue("@Type", DBNull.Value);
      con.Open();
      cmd.ExecuteNonQuery();
      con.Close();
    }

  }


  return View(form);
}
//
    
asked by E.Rawrdríguez.Ophanim 10.11.2017 в 22:51
source

1 answer

2

You will need to modify your submission script slightly, making append of the files that you have collected in your form.

In SO there is a very interesting answer , which runs through the entire form, in case there are several files in it and you would like send them all.

This part can be modified, indicating only one specific file if desired.

<script>
  function tryx() {
      var form = new FormData();
      $.each($('#uploadForm')[0].files, function(i, file) {
        form.append('file-'+i, file); //Aquí se agrega un identificador a cada file, no sé si sea útil en tu caso
      });
     console.log(form);

    $.ajax({
      url: '@Url.Action("KyoTest", "Home")',
      method: "POST",
      dataType: 'json',
      data: form,
      processData: false,
      contentType: false,
      success: function(result) {
        alert("at least Ajax looks fine" + response);
      },
      error: function(er) {
        console.log(er);
        alert("something got wrong" + er);
      }
    });

  }
</script>

Notes:

  • Review the information in the console, the for may be creating well
  • Verify that the URL is correct, the request could fail because of a URL not found
  • Verify that the Ajax request is handled well on the server, that the eventual file that receives it has no errors.
answered by 10.11.2017 / 23:36
source