【发布时间】:2016-06-04 03:25:13
【问题描述】:
我正在为一个小型网页进行上传项目,我在本地完成了所有工作,我可以上传文件,我可以操作它们,并且我可以获得必要的数据作为响应。我昨晚把整个事情都推到了天蓝色上,现在当我尝试上传一些东西时,我得到了
POST mysite/uploads 404 (Not Found)
我的文件.html
<form id="uploadForm"
enctype="multipart/form-data" action="/uploads" method="post">
<input type="file" name="userPhoto" class="btn btn-wire" />
<input type="submit" value="Upload" name="submit" class="btn btn-primary" />
</form>
html-file.js
jQuery(document).ready(function () {
$('#uploadForm').submit(function () {
$("#status").empty().text("File is uploading...");
$(this).ajaxSubmit({
error: function (xhr) {
$("#status").text('Error: ' + xhr.status);
//alert(JSON.stringify(xhr));
},
success: function (response) {
// var test = JSON.stringify(response[1], null, 4);
// console.log(JSON.stringify(response, null, 4));
files = [];
$(response).each(function (index) {
files.push(response[index]);
})
refreshFiles();
alert("alertsuccess");
$("#status").empty().text("File is uploaded...");
}
});
return false;
});
});
nodeapp.js
app.use(multer({
dest: "./uploads",
onFileUploadStart: function (file) {
console.log(file.originalname + ' is starting ...');
},
onFileUploadComplete: function (file) {
values = JSON.stringify(file);
console.log(values);
test.push({ "url": file.path, "filename": file.name, "fileType": file.extension });
}
}));
app.post('/uploads'), function (req, res) {
upload(req, res, function (err) {
if (err) {
return res.end("Error uploading the file!");
}
else {
//res.json({ "test": "test123" });
res.json(test)
// res.write(JSON.stringify(test, null, 4));
res.end();
test = [];
}
})
})
还有很多代码,但为了简单起见和我对项目的这个特定部分的普遍怀疑,我只发布了这些 sn-ps。
【问题讨论】:
-
新路由推送后你重启应用了吗?
-
第一次推送我觉得没必要,但是在你评论后我去重新启动应用程序,没有任何改变。问题依然存在。
-
Typo app.post('/upload')
-
即使我将 app.post 放在 app.use 之外,我仍然会遇到同样的错误。
-
另外 app.post 应该在 app.use(multer 之外,否则在调用 onFileUploadComplete 之前发布路由 /uploads 不存在,app.post('/uploads...每次文件上传完成时都会重复调用这是错误的。
标签: javascript html node.js azure http-status-code-404