2011-09-23 38 views
5

我的javascript功能只能正確上傳文本文件。任何人都可以幫我弄清楚如何讓它也接受圖像等?Javascript未上傳二進制數據

function fileUpload(files) { 
    if (!files.length) { 
    fileList.innerHTML = "<p>No files selected!</p>"; 
    } else {  
var list = document.createElement("ul"); 

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

    //Set vars 
    var file = files[i], 
    fileName = file.name, 
    fileSize = file.size, 
    fileData = file.getAsBinary(), 
    boundary = "xxxxxxxxx", 
    uri = "receive.php", 

    //Create file info HTML output 
    li = document.createElement("li"); 
    list.appendChild(li); 
    var info = document.createElement("span"); 
    info.innerHTML = file.name + ": " + file.size + " bytes"; 
    li.appendChild(info); 

    //Start sending file 
    var xhr = new XMLHttpRequest(); 
    xhr.open("POST", uri, true); 
    xhr.setRequestHeader("Content-Type", "multipart/form-data, boundary="+boundary); // simulate a file MIME POST request. 

    xhr.onreadystatechange = function() { 
    if (xhr.readyState == 4) { 
     if ((xhr.status >= 200 && xhr.status <= 200) || xhr.status == 304) { 

     if (xhr.responseText != "") { 
      alert(xhr.responseText); // display response. 
     } 
     } 
    } 
    } 

    var body = "--" + boundary + "\r\n"; 
    body += "Content-Disposition: form-data; name='upload'; filename='" + fileName + "'\r\n"; 
    body += "Content-Type: application/octet-stream\r\n\r\n"; 
    body += fileData + "\r\n"; 
    body += "--" + boundary + "--"; 

    xhr.send(body); 

} 
fileList.appendChild(list); 
return true; 
    } 
} 

更新:我發現下面的功能在網上http://code.google.com/p/html5uploader/但我無法弄清楚如何將其應用到我的當前功能。 xhr.sendAsBinary是唯一改變了的事情嗎?

// Upload image files 
upload = function(file) { 

    // Firefox 3.6, Chrome 6, WebKit 
    if(window.FileReader) { 

     // Once the process of reading file 
     this.loadEnd = function() { 
      bin = reader.result;     
      xhr = new XMLHttpRequest(); 
      xhr.open('POST', targetPHP+'?up=true', true); 
      var boundary = 'xxxxxxxxx'; 
      var body = '--' + boundary + "\r\n"; 
      body += "Content-Disposition: form-data; name='upload'; filename='" + file.name + "'\r\n"; 
      body += "Content-Type: application/octet-stream\r\n\r\n"; 
      body += bin + "\r\n"; 
      body += '--' + boundary + '--';  
      xhr.setRequestHeader('content-type', 'multipart/form-data; boundary=' + boundary); 
      // Firefox 3.6 provides a feature sendAsBinary() 
      if(xhr.sendAsBinary != null) { 
       xhr.sendAsBinary(body); 
*snip* 
+0

你如何檢索文件。我的意思是文件參數必須在某處提供。你是怎樣做的?我需要通過ajax來解釋這個文件上傳的問題。任何幫助將不勝感激。 – user1575229

+0

@ user1698985我不確定你的意思,你的意思是在PHP端檢索它?在這種情況下,您可以在POST數據中找到它。 – natli

+0

在服務器端檢索它是另一個討論。我只想找到加載到表單數據並通過ajax發送的客戶端上選擇文件的方式。在Html5中很容易。但我不明白它是如何在以前的版本的HTML。我試圖窺探ajaxform插件,但很難理解它是如何檢索文件數據並通過ajax發送的。我想了解整個過程,從jquery中選擇文件到處理並提交給服務器。 – user1575229

回答

2

有一個在http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2發送使用multipart/form-data GIF圖像的W3C例如:

 
Content-Type: multipart/form-data; boundary=AaB03x 

--AaB03x 
Content-Disposition: form-data; name="submit-name" 

Larry 
--AaB03x 
Content-Disposition: form-data; name="files" 
Content-Type: multipart/mixed; boundary=BbC04y 

--BbC04y 
Content-Disposition: file; filename="file1.txt" 
Content-Type: text/plain 

... contents of file1.txt ... 
--BbC04y 
Content-Disposition: file; filename="file2.gif" 
Content-Type: image/gif 
Content-Transfer-Encoding: binary 

...contents of file2.gif... 
--BbC04y-- 
--AaB03x-- 

通知額外線Content-Transfer-Encoding: binary。嘗試添加。

編輯:嘗試Base64編碼,使用the Base64 jQuery plugin文件數據:

var body = "--" + boundary + "\r\n"; 
    body += "Content-Disposition: form-data; name='upload'; filename='" + fileName + "'\r\n"; 
    body += "Content-Type: application/octet-stream\r\n"; 
    body += "Content-Transfer-Encoding: base64\r\n\r\n"; 
    body += $.base64Encode(fileData) + "\r\n"; 
    body += "--" + boundary + "--"; 
+0

不幸的是,這並沒有改變一件事。文本文件仍然正確上傳,但圖像不正確。圖像最終損壞了服務器,大約是正常文件大小的兩倍。然而,我確實在網上找到了一個我想要的功能,但由於我是JavaScript新手,我不知道如何將它實現到我自己的功能中。 (查看更新的問題)由於我缺乏經驗,我也不明白你的問題,爲什麼它必須是body.length? – natli

+0

@natli:[因爲Firefox顯然是唯一支持'sendAsBinary()'的瀏覽器](http://stackoverflow.com/q/4236153/196844),它看起來像是使用FF的內置'sendAsBinary() '方法,但在Chrome和Safari上,他們實現了一個解決方法(儘管只是猜測)。無論如何,二進制文件應該先用base64編碼。然後設置「Content-Transfer-Encoding:base64」。就Content-Length而言,這是不能使用'setRequestHeader()'方法設置的標題之一(http://www.w3.org/TR/XMLHttpRequest/#the-setrequestheader-method)。所以,不要擔心。 –

+0

@natli:我更新了我的答案。 –