尝试在发布路线内更新。这条路线是从表单中调用的。`
html
head
title File upload Node.
body
form#uploadForm(enctype='multipart/form-data', action='/api/photo', method='post')
input(type='file', name='userPhoto')
input(type='submit', value='Upload Image', name='submit')
script(src='http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js')
script(src='http://cdnjs.cloudflare.com/ajax/libs/jquery.form/3.51/jquery.form.min.js')
script.
$(document).ready(function() {
$('#uploadForm').submit(function() {
$(this).ajaxSubmit({
error: function(xhr) {
status('Error: ' + xhr.status);
},
success: function(response) {
console.log(response);
}
});
return false;
});
});
---------------------------ur sever file-------------
app.use(multer({ dest: './uploads/',
rename: function (fieldname, filename) {
return filename+Date.now();
},
onFileUploadStart: function (file) {
console.log(file.originalname + ' is starting ...')
},
onFileUploadComplete: function (file) {
console.log(file.fieldname + ' uploaded to ' + file.path)
done=true;
}
}));
app.get('/',function(req,res){
res.sendfile("index.html");
});
app.post('/api/photo',function(req,res){
if(done==true){
console.log(req.files);
res.end("File uploaded.");
}
});