0

I create a form input file to let the user browse their file and upload to the server After that I use Jquery to get the data of the file and try to use AJAX to send the data of the file to the Node router to process. However the data file after sending becoming an { object File } and I don't know how to process the data.
Below is my HTML and AJAX code to send file data
HTML:

<input type="file" accept="image/png, image/jpeg, image/gif" id="feature-image" />  

AJAX:

$("#feature-image").on("change", function () {
    var featureImageFile = $('#feature-image').get()[0].files[0];  
    $.ajax({  
        type: 'GET',
        data: {file: featureImageFile } ,
        url: '/test',
        contentType: false,
        processData: false,
        success: function() {
            console.log("OK");
        }
     });
});

SERVER:

router.get("/test", (req, res) => {
    var file = req.query;
    console.log(file);
});

When I print out the file, it returns me an object: {object File} and I don't know how to deal with this. I want to get the data to store the the file using fs library to the server. Please help me !

Quan Bui
  • 1
  • 1
  • 4
  • See this https://stackoverflow.com/a/8758614/3284355 you need to use something like `data: new FormData($('#feature-image').get()[0].files[0])` – Molda Jan 12 '19 at 09:41
  • @molda I have tried FormData, however, it also return me an {"object FormData": ""} and I don't know how to extract or deal with this object – Quan Bui Jan 12 '19 at 10:00
  • Possible duplicate of [Sending multipart/formdata with jQuery.ajax](https://stackoverflow.com/questions/5392344/sending-multipart-formdata-with-jquery-ajax) – Jude Fernandes Jan 12 '19 at 12:00

0 Answers0