Uploading files via jQuery, object FormData is provided and no file name, GET request

后端 未结 4 2041
慢半拍i
慢半拍i 2021-01-13 01:27

I am using a jQuery script to upload files to a new page. It somehow works too, but the issue is that it sends the form data as object FormData.

Here i

相关标签:
4条回答
  • 2021-01-13 01:58

    First get the Object

    First get the Object from HTML
    
     //HTML
     <input id = "file_name" type = "file" />
    
     //JS
     var formData = new FormData()
     var file_obj = document.getElementById("file_name")
     formData.append('file_name', file_obj.files[0]);
     $.ajax({
        url: url,
        type: 'POST',
        data: formData,
        success: function (data) {
    
        },
        cache: false,
        contentType: false,
        processData: false
    })
    
    0 讨论(0)
  • 2021-01-13 02:09

    Files cannot be uploaded with the GET method. You need to use POST.

    $.ajax({
       method: 'POST',
       url: '/test/file_capture',
       // ...
    

    Also, you need HTML 5 to be able to upload files (though Firefox might allow it with earlier XHTML).

    0 讨论(0)
  • 2021-01-13 02:10

    you should only submit the file - not the complete form

    var fileInput = $('#image');
    var file = fileInput.files[0];
    var formData = new FormData();
    formData.append('file', file);
    

    EDIT

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    
    <form enctype="multipart/form-data">
      <input type="file" id="image" name="image" accept="Image/*" />
      <input type="submit" id="submit" name="" value="Upload" />
    </form>
    
    <script>
    $('#submit').click(function (event) {
        event.preventDefault()
       var file = $('#image').get(0).files[0];
       var formData = new FormData();
       formData.append('file', file);
       $.ajax({
           url: '/test/file_capture',
           //Ajax events
           beforeSend: function (e) {
             alert('Are you sure you want to upload document.');
           },
           success: function (e) {
             alert('Upload completed');
           },
           error: function (e) {
             alert('error ' + e.message);
           },
           // Form data
           data: formData,
           type: 'POST',
           //Options to tell jQuery not to process data or worry about content-type.
           cache: false,
           contentType: false,
           processData: false
        });
        return false;
    });
    </script>
    
    0 讨论(0)
  • 2021-01-13 02:12

    You need to explicitly get the file.

    var image = $('#image')[0].files[0];
    

    And then append the file to formData:

    formData.append( image );
    

    Here's an example of how I do it:

        var image = $('#image')[0].files[0];
    
        if( window.FormData ) {
            formdata = new FormData();
            formdata.append( 'image', image );
            formdata.append( 'action', 'save-image' );
    
            $.ajax({
                url: 'controller/handler',
                type: 'POST',
                data: formdata,
                processData: false,
                contentType: false,
                success: function( res ) {
                    // Handle it.
                }
            });
        }
    }
    
    0 讨论(0)
提交回复
热议问题