2013-10-02 19 views
0

我做了一些拖放功能來保存圖像,我想看看我剛剛保存的圖像。 問題是,我不會等Ajax請求將圖像發送到服務器,我想要圖像的即時預覽。我做了可以做到這一點的代碼,但是當我向它添加一個Ajax請求時,直到完成一個Ajax請求後,我纔在HTML頁面上看到這個圖像。但是,如果我會評論這個Ajax請求,圖像立即出現在html頁面上。我怎麼能得到它所有的? Ajax +即時預覽。Ajax請求會減慢函數的其餘部分,如何解決?

這裏是我的代碼,隨時問我這件事:

// initialise our dropzone and set ondrop event function 
var dropzone = document.getElementById('ta'); 

dropzone.ondrop = function(e){ 
    e.preventDefault(); 
    readfiles(e.dataTransfer.files); 
}; 

// preview image that was added to dropzone 
function previewImg(file) { 

    var image = new Image(); 


    image.src = webkitURL.createObjectURL(file);// make url for image using webkitURL 

    image.width = 550; // a fake resize 

    document.getElementById('body').appendChild(image); // add image to element (for example to the body) 

}  

function readfiles(files) { 
    var formData = new FormData(); 

    for (var i = 0; i < files.length; i++) { 

    previewImg(files[i]); // call for preview 

    formData.append('file'+i, files[i]); // add file to form 


    } 
    formData.append('moreInfo','myValuableValue');// you can append additional string info 

    // if we comment this request preview will appear instant 
    // but if we not, preview will appear only after end of ajax request 
    $.ajax({ 
    url: './file_handler.php', 
    type: 'POST', 
    data: formData, 
    async: false, 
    success: function (data) { 
     console.log('done'); 
    }, 
    cache: false, 
    contentType: false, 
    processData: false 
    }); 


}  

我想解決這個問題,但我不知道我怎麼了,使用jQuery $就要求。順便說一下,控制檯輸出也會變慢,並在請求結束後立即顯示。請告訴我方式。

回答

2

在您傳遞給您的$ .ajax調用的設置中,設置爲async: true

+0

天哪這是soooo跛腳,謝謝 –

相關問題