I am using dropzone.js to implement file uploading. I am new to JavaScript. Can anyone tell me what does the variable file
store here? The name of the file?
accept: function(file, done) {
return done();
},
Moreover, I want to display the content of the file to the text area having id="editor"
.
accept: function(file, done) {
document.getElementById('editor').value=file.text;
return done();
},
Ihave used file.text
and also the readAsText()
, but both of them are showing undefined
in the text area. Is there any other easy way to do it?
This function is present in dropzone.js
file.
OR is there any way so that i can use PHP functions like file_get_contents()
in .js
file and in JavaScript function ?
The file parameter is an object - Just use console.log to check what data the object returns.
accept: function(file, done) {
console.log(file);
done();
},
If you are uploading your file through PHP and want to return the URL of the uploaded file to display in the text area, try this instead -
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);
});
}
Where file is the object and data is the server response (You could return the URL of the uploaded file in PHP after passing the data to it as you would a regular form). I noticed you also tagged PHP, so I assume this is the intended purpose.
See http://www.dropzonejs.com/#configuration for usage of accept.
Edit:
To display the contents of a text file try this with your code -
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();
}