Hi I have problem with upload image in jquery. When i uplaod image ,show me errors. How do i solve this peroblem ?
I can't use the form to dropzone because it is in another form. Errors:
POST http://localhost:3000/upload 400 (Bad Request)
Object {error: Object}
code html
<div class="dropzone" action="/upload" encType="multipart/form-data" method='post' id="id_dropzone" >
<div class="dz-message">
ِDrop file here to upload
</div>
</div>
Code JQUERY
$(document).ready(function(){
Dropzone.autoDiscover = false;
$("#id_dropzone").dropzone({addRemoveLinks: true, autoQueue: true,paramName: "file", maxFilesize: 2, maxFiles: 1,
success: function (file, response) {
var imgName = response;
file.previewElement.classList.add("dz-success");
console.log("Successfully uploaded :" + imgName);
},
error: function (file, response) {
file.previewElement.classList.add("dz-error");
console.log(response);
},
maxThumbnailFilesize: 1,acceptedFiles: "image/*" });
})
Code app.js
var express = require('express');
var routes = require('./routes')();
var fileUpload = require('express-fileupload');
var path = require('path');
var app = express();
app.use(fileUpload());
app.use('/files', express.static(__dirname + '/files'));
.
.
.
app.post('/upload', function(req, res) {
var sampleFile;
if (!req.files) {
res.send('No files were uploaded.');
return;
}
sampleFile = req.files;
console.log(sampleFile);
sampleFile.file.mv(__dirname + '/files/'+sampleFile.file.name,
function(err) {
if (err) {
console.log(err);
res.status(500).send(err);
} else {
res.send('File uploaded!');
}
});
});
package.json
{
"name": "application-name",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node app.js"
},
"dependencies": {
"connect-mongo": "~0.3.3",
"ejs": "^2.5.6",
"express": "^3.21.2",
"express-fileupload": "^0.1.4",
"mongoose": "~3.6.19"
},
"devDependencies": {
"mocha": "~1.12.1",
"should": "~1.2.2",
"supertest": "~0.7.1"
}
}
I solved this problem with the following code.
app.js
I have made a working example based on your code, and I could not reproduce the error. File get successfully uploaded.
Only difference is that I have moved
Dropzone.autoDiscover = false;
line beforedocument.ready
block.Here is a full working source based on your code
server.js
index.html
package.json