2017-01-17 40 views
0

我生成jQuery中的下一個形式:

$('.content').append(' 
<form name="make_new_model_release" enctype="multipart/form-data"> 
<input data-validate="validate" type="text" name="new_model_release_title" placeholder="Enter new model release title" /> 
<input type="file" name="newModelReleaseFile" id="newModelReleaseFile" /> 
<input type="submit" value="Create new model release" /> 
</form>'); 

服務器側簡單:

var_dump($_FILES); 

AJAX代碼:

var data = form.serialize(); 
$.ajax({ 
    type: 'POST', 
    dataType: 'json', 
    url: formurl, 
    data: data, 
    beforeSend: function(data) { 
     form.find('input[type="submit"]').attr('disabled', 'disabled'); 
    }, 
    success: function(data) { 
     console.log(data); 
    }, 
    complete: function(data) { 
     form.find('input[type="submit"]').prop('disabled', false); 
    } 
}); 

後提交$ _FILES數組是空的。

  1. 我檢查的php.ini

file_uploads = ON | upload_max_filesize = 128M |的post_max_size = 128M

  • 臨時文件夾被允許用於讀出和寫入

  • 我試圖使data: new FormData(formId) - 什麼都沒有改變,$ _FILES數組爲空。

  • +0

    我看不到你的'formId' –

    +0

    我higly推薦爲[閱讀](https://developer.mozilla.org/en-US/docs/ Web/API/XMLHttpRequest/Using_XMLHttpRequest#Submitting_forms_and_uploading_files)。我用它通過ajax上傳文件,工作。 – xmike

    回答

    0

    如果使用jQuery('#dailyActivity').serialize()
    它是不工作的<input type'file'>
    有看看這個jsFiddle不工作

    這一個.serialize()

    從文件數據選擇元素不是序列化的。

    有看看這個https://stackoverflow.com/a/8758614/3425489

    在你的情況下嘗試這種

    要發送<input type'file'>你可能想試試這個

    var formData = new FormData($('form')[0]); 
    

    或指定準確數據FORMDATA

    var formData = new FormData(); 
    // Append your other Data 
    formData.append('newModelReleaseFile', $('input[type=file]')[0].files[0]); 
    

    你Ajax調用會

    $.ajax({ 
        type: 'POST', 
        url: formurl, 
        data: formData, 
        // THIS MUST BE DONE FOR FILE UPLOADING 
        contentType: false, 
        processData: false, 
        beforeSend: function(data) { 
         form.find('input[type="submit"]').attr('disabled', 'disabled'); 
        }, 
        success: function(data){ 
         console.log(data); 
        }, 
        complete: function(data) { 
         form.find('input[type="submit"]').prop('disabled', false); 
        } 
    }); 
    
    +0

    謝謝。是工作。 – user3774771