当input file 状态更改的时候发送ajax

Published
2023-03-03
浏览次数 :  113

$(document).ready(function() {
  // Get the input file element
  var inputFile = $('input[type="file"]');

  // Bind the change event handler to the input file element
  inputFile.change(function() {
    // Construct a FormData object to send with the request
    var formData = new FormData();
    formData.append('file', inputFile[0].files[0]);

    // Send the AJAX request
    $.ajax({
      url: 'upload.php', // Replace with your own endpoint
      type: 'POST',
      data: formData,
      processData: false,
      contentType: false,
      success: function(response) {
        console.log('File uploaded successfully:', response);
      },
      error: function(jqXHR, textStatus, errorThrown) {
        console.log('Error uploading file:', textStatus, errorThrown);
      }
    });
  });
});

  • 标签1
  • 标签1
  • 标签1
Top