我有一个包含多个描述文本字段、文件选择字段和跨域请求(CORS 变体)的提交按钮的表单:
<form class="upload" id="upload_form" enctype="multipart/form-data" method="post" action="http://localhost:3001/upload/1234567890">
<div class="row-fluid">
<div class="span5 row-fluid" id="description" style="margin-left:0px;">
<div>
<label>Title</label>
<input class="span12" type="text" placeholder="Title" id="description_title" name="description_title"/>
<label>Author</label>
<input class="span12" type="text" placeholder="Author" id="description_author" name="description_author"/>
<label>Tags</label>
<input class="span12" type="text" placeholder="Tags" id="description_tags" name="description_tags"/>
<label>Description</label>
<textarea class="span12" id="description_textarea" name="description_textarea" rows="5" style="resize:none"></textarea>
<div id="buttons" class="row-fluid" style="margin-top: 5px">
<div class="span12">
<span class="span5 btn btn-primary btn-file" id="chose_files_btn" onclick="filechose_button.click()">chose files
<input id="filechose_button" type="file" name="fileData" data-url="http://localhost:3001/upload/1234567890"/></span>
<button id="upload_button" type="submit" name="upload" class="span5 offset2 btn btn-success" disabled="true" onclick="$('#upload_form').trigger('upload_fired');">upload</button>
</div> <!-- span12 -->
</div> <!-- buttons -->
</div> <!-- well -->
</div> <!-- video_description -->
</div> <!-- row-fluid -->
</form>
Node.js (Express.js) 服务器有一个路由:
app.post('/upload/:id', function(req, res){
console.log(req.fields);
...
});
问题是,我无法从输入字段中找到数据:req.fields
gets undefined
。
如何从req
服务器上找到数据(文本描述和文件)?
更新:
伟大的!req.body
真的可以访问所有领域,但是:
console.log(req.body)
印刷:
{ description_title: 'aaa',
description_author: 'bbb',
description_tags: 'ccc',
description_textarea: 'ddd',
upload: '' }
但是fileData
领域在哪里?我的意思是,我上传的带有描述(上述字段)的文件在哪里?
更新 2:完成!
文件不在body属性中,而是在req.files
感谢亚当和安迪凡!