Upload image using jQuery, AJAX and PHP

前端 未结 2 956
臣服心动
臣服心动 2021-01-28 02:20

I\'m trying to choose an image from PC/mobile and upload it to the server on one button click but for some reason I can\'t grab the value (name) of chosen image - $_FILES[

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-28 02:37

    You need to populate your FormData() with your file data:

    var formData = new FormData();
    $.each($('pictureCapture')[0].files, function(key, value) {
        formData.append(key, value);
    });
    

    Add change your contentType to multipart in your jQuery ajax call:

    $.ajax({
        ...
        data: formData, 
        ...
        contentType: 'multipart/form-data',
        ...
    

提交回复
热议问题