2011-07-02 10 views
1

xmlhttprequest.responseTextのresponseTextが空になっているのはなぜですか?ajaxを使用してファイルをアップロードしていますか?xmlhttprequestの空のresponseText

マイコード:XMLHttpRequestオブジェクトで

req = new XMLHttpRequest(); 
req.file = file; 
req.addEventListener('change', changeProgress); 
req.onreadystatechange = 
function() { 
if(this.readyState == 4) { 
    //etc.. 
alert(req.responseText); 
} 
}; 
req.open('POST','/upload',true); 
req.send(file); 

答えて

2

アップロードファイルは、セキュリティ上の理由から、サポートされていません

EDIT:これはXMLHttpRequest 2

function upload(blobOrFile) { 
    var xhr = new XMLHttpRequest(); 
    xhr.open('POST', '/server', true); 
    xhr.onload = function(e) { ... }; 

    // Listen to the upload progress. 
    var progressBar = document.querySelector('progress'); 
    xhr.upload.onprogress = function(e) { 
    if (e.lengthComputable) { 
     progressBar.value = (e.loaded/e.total) * 100; 
     progressBar.textContent = progressBar.value; // Fallback for unsupported browsers. 
    } 
    }; 

    xhr.send(blobOrFile); 
} 

upload(new Blob(['hello world'], {type: 'text/plain'})); 
+0

もしかしてと、しかし、可能です、私はアップロードを行うことはできますが、AjaxのresponseTextにアクセスすることはできませんか? –

+0

彼は 'XMLHttpRequest'オブジェクトを使ってファイルをアップロードできないことを意味します – nrathaus

+0

これはもはや正確ではありません。 http://www.w3.org/TR/XMLHttpRequest2/ –

関連する問題