2014-09-11 60 views
0

我們正在談論的文件類型拉鍊讀取,而輸入按鈕的javascript

<input type="file" id="file-input" name="files[]" accept="application/zip"/> 

守則道場的文件..

fileInput = document.getElementById("file-input"); 
        fileInput.addEventListener('change', function() { 
         console.log("File success"); 
         console.log(fileInput.files[0]); 
         zipParser(fileInput.files[0]); 
        },false); 

意味着上點擊按鈕得到發現者/ explorer..select zip文件,並在改變聽者我們獲取文件的實例

的console.log顯示

File success 
tpk-layer1.html:168 File {webkitRelativePath: "", lastModifiedDate: Tue Sep 09 2014 11:50:18 GMT+0530 (IST), name: "Beirut.zip", type: "application/zip", size: 14263592…}lastModifiedDate: Tue Sep 09 2014 11:50:18 GMT+0530 (IST)name: "Beirut.zip"size: 14263592type: "application/zip"webkitRelativePath: ""__proto__: File 

我不想按鈕實例。那是這個的onclick和聽者需要刪除,並應直接onload事件頁面的調用文件..

我如何能實現this.Please讓我知道如果任何澄清是需要

更多信息

我已經打過電話

zipParser('/samples/tpks/Beirut.zip'); 

但得到的迴應是

Uncaught TypeError: Failed to execute 'readAsArrayBuffer' on 'FileReader': The argument is not a Blob. 

請注意,這是PhoneGap的應用

+0

你怎麼會知道在網頁加載加載哪些文件? – CodingIntrigue 2014-09-11 11:52:29

+0

這是一個zip文件,我有一個單獨的文件可以加入源代碼 – 2014-09-11 11:54:46

+0

Yikes,最後一點是一個很大的編輯。我已經添加phonegap作爲標籤,因爲如果您想要從設備中提取文件,AJAX將無法正常工作。 – CodingIntrigue 2014-09-11 12:00:48

回答

1

如果您需要一個blob,您可以使用XMLHttpRequest:

var xhr = new XMLHttpRequest(); 
xhr.onreadystatechange = function(){ 
    if (this.readyState == 4 && this.status == 200){ 
     var blob = this.response; 
     // Call zipParser? 
     zipParser(blob); 
    } 
} 
xhr.open('GET', './yourZipFile.zip'); 
xhr.responseType = 'blob'; 
xhr.send(); 
+0

男人..那種理由震撼!非常感謝 – 2014-09-11 12:36:14

+0

@ LithuT.V沒問題,在黑暗中狂野刺傷的時候不錯! – CodingIntrigue 2014-09-11 12:36:52