[英]file variable in dropzone.js
我正在使用dropzone.js來實現文件上傳。 我是JavaScript新手。 誰能告訴我變量file
在這里存儲了什么? 文件名?
accept: function(file, done) {
return done();
},
此外,我想將文件的內容顯示到具有id="editor"
的文本區域。
accept: function(file, done) {
document.getElementById('editor').value=file.text;
return done();
},
我使用過file.text
和readAsText()
,但是它們兩個在文本區域中都顯示為undefined
。 還有其他簡單的方法嗎?
該功能存在於dropzone.js
文件中。
或者有什么方法可以讓我在.js
文件和JavaScript函數中使用PHP函數,例如file_get_contents()
?
file參數是一個對象-只需使用console.log檢查該對象返回什么數據。
accept: function(file, done) {
console.log(file);
done();
},
如果您要通過PHP上傳文件,並希望返回上傳文件的URL以顯示在文本區域中,請嘗試以下操作-
init: function() {
this.on("success", function(file, data) {
// Log both the file object and server response
console.log(file);
console.log(data);
// Remove the file from the dropzone so it doesn't clutter the UI
this.removeFile(file);
});
}
其中file是對象,data是服務器響應(您可以像常規形式一樣將數據傳遞給它后,可以用PHP返回上傳文件的URL)。 我注意到您也標記了PHP,所以我認為這是預期的目的。
有關accept的用法,請參見http://www.dropzonejs.com/#configuration 。
編輯:
要顯示文本文件的內容,請嘗試使用您的代碼-
var editor = document.getElementById('editor');
accept: function(file, done) {
// Get the file contents - Load result into element
var reader = new FileReader();
reader.onload = function(e) {
editor.innerText = reader.result;
}
reader.readAsText(file);
// Return done
done();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.