如何从未定义的表单中获取req.body?

时间:2016-04-30 01:59:49

标签: javascript node.js express fs

每当我提交包含信息的表单时,它都会以未定义的形式返回。我已经发布了以下代码。如果我在我的表单中包含(enctype =“multipart / form-data”)我没有收到任何身体(req.body)。但是,如果我不包含它,我收到一个正文,但文件处理不起作用,页面只是继续加载。

app.post('/processupload', function(req, res) {
var date = new Date();
titles.push(req.body.pTitle);
descriptions.push(req.body.postDescription);
dates.push(date.toString());
file_names.push(req.body.fUpload);

console.log(req);

var form = new formidable.IncomingForm();
form.parse(req, function(err, fields, files)
{
    if(err) return res.redirect(303, '/error');
}); 

form.on('end', function(fields, files)
{
    var temp_path = this.openedFiles[0].path;
    var file_name = this.openedFiles[0].name;
    var new_location = __dirname + '/public/images/';
    fs.copy(temp_path, new_location + file_name);
    res.redirect(303, 'home');
});

});

<form action="/processupload" enctype="multipart/form-data" method="POST" id="uploadForm" name="postForm">
    <p align="center" id="pUploadForm" name="pPostForm"><label for="photoTitle">Photo Title: </label>
    <input type="text" id="photoTitle" name="pTitle"><br>

    <br><input type="file" id="fileUpload" name="fUpload"><br>

    <br><label for="photoCaption">Photo Caption: </label><br>
    <textarea rows="10" cols="50" id="photoCaption" name="postDescription"></textarea><br><br>
    </p>
</form> 

1 个答案:

答案 0 :(得分:0)

几个星期前我创建了一个有照片上传的项目。我使用了Angular和Node。但是,如果没有Angular,它仍然可以使用Node。我使用了multer npm包。

    <form action="/testupload" method='post' enctype='multipart/form-data'>
        <input type="file" name="photo" id="photo" multiple=false>
        <button type="submit">Submit</button>
    </form>

这有助于我上传图片,然后从S3 Bucket返回图片网址。但是您可以根据需要处理该文件。 Multer允许您访问name="photo",这样您就可以做任何您需要做的事情,在这个例子中我创建了一个唯一的id,以便将url发送回前端,因此以某种方式使用它来源。这是使用此代码的表单:

upload.single('photo')

虽然SUM(IF(MATCH($A$2:$A$21,$A$2:$A$21,0)=ROW($A$2:$A$21)-1, NOT(ISERROR(MATCH($A$2:$A$21&$E2,$A$2:$A$21&$B$2:$B$21,0)))*NOT(ISERROR(MATCH($A$2:$A$21&F$1, $A$2:$A$21&$B$2:$B$21,0))), 0))中间件必须反映表格中<!DOCTYPE html> <html> <head> <script> function createdTextFile(){ var date = new Date(); var file = new File(["text text text text text text"], "textfile.txt", {type: "text", lastModified: date}); /* sent file to server var formdata = new FormData(); formdata.append("Uploaded_file", file); */ //Created download link var uriContent = "data:application/octet-stream,"+file; var div_idFileDownload = document.getElementById("file_dowload"); div_idFileDownload.innerHTML = ""; var createElement_aFileDownload = document.createElement("a"); createElement_aFileDownload.setAttribute("download", "textfile.txt"); createElement_aFileDownload.setAttribute("href", uriContent); div_idFileDownload.appendChild(createElement_aFileDownload); var nm = document.createTextNode("textfile.txt"); createElement_aFileDownload.appendChild(nm); } //open text file: [objectFile] //Need the text file to say: text text text text text text </script> </head> <input type="file" id="upload_img"> <input type="button" onclick ="createdTextFile()" value="download"> <div id="file_dowload"><div/> </html> 需要很长时间才能调试的重要内容。我希望这会有所帮助,有很多方法可以解决这个问题,这只是一个。

来源: https://www.npmjs.com/package/multer http://docs.aws.amazon.com/AmazonS3/latest/UG/UploadingObjectsintoAmazonS3.html