我有一个html:解析:发帖上传的文件/图片后端
form.html:
<form method="post" enctype="multipart/form-data" action="/">
<input type="file" name="pic" id="imgupload">
</form>
<script>
document.getElementById("imgupload").onclick = function({myFunction()};
function myFunction() {
var fileUploadControl = $("#imgupload")[0];
if (fileUploadControl.files.length > 0) {
var file = fileUploadControl.files[0];
var name = "photo.jpg";
//how to pass this (parseFile) to server.js?
var parseFile = new Parse.File(name, file);
}
}
</script>
和我使用的后端服务器代码快递:
简化server.js(只服务器的路由):
app.get('/', (req, res) => {
//loads the form.html from above.
res.sendFile(__dirname + "/form.html");
})
app.post('/', (req, res)=>{
//once press submit button, will post parseFile to here
//from here, i will save the parseFile to database.
res.status(204).end();
})
我的目标是上传图片,那么当用户按下提交,该parseFile变量将被发送到发生保存的server.js中的app.post()。
我试过multer,它不适合Parse.File工作在req.file即:
app.post('/', upload, (req,res)=>{
//req.file contains the file information
var file = new Parse.File(name, req.file);
//save to database via Parse
file.save(); //example code.
res.status(200).success("test code")
})
此代码返回类型错误在哪里解析是Parse.File无法创建一个解析。来自req.file的文件。
任何帮助非常感谢!
谢谢先进!