如何获取和本地存储从前端上传的文件

时间:2016-02-04 11:43:43

标签: node.js

我有一个HTML代码,它接受批名称(下拉列表),参数(textarea),电子邮件ID(测试)和批处理文件.txt输入文件(文件)。当我在前端表单中单击“提交”时,我会在日志中看到以下内容:

GET /batch?batches=Batch1 - - ms - -

我不知道怎样才能知道并提取&通过NodeJS脚本在服务器端存储文件名?还有什么方法来捕获用户提交的textarea?

<form action="batch">
<label for="batch">Select Batch&nbsp;&nbsp;</label>
<select name="batches" id="batch">
<select name="batches" id="batch">
    <option value="" disabled="disabled" selected="selected">Please select a batch</option>
    <option value="1">Batch1</option>
    <option value="2">Batch2</option>
    <option value="3">Batch3</option>
    <option value="4">Batch4</option>
    <option value="5">Batch5</option>
</select>
</p>
<br/>
<p>
<label for="binpfile">Batch Input File</label>
<input type="file" id="binpfile" accept=".txt">
</p>
<br/>
<p>
<label for="emailid">E-Mail ID:</label>
<input type="text" id="emailid"><br>
</p>
<br/>
<p class="formfield">
<label for="bparam">Batch Parameters:</label>
<textarea rows="4" cols="50" id="bparam" form="usrform">Enter batch param here...</textarea>
<input type="submit" value="Submit">
</form>

下面是我的NodeJS代码:我在日志中看到“用户输入的Batch1 undefined undefined undefined”

exports.processbatch = function(req, resp) {
    var bname = req.query['batches'];
    var fname = req.query['binpfile'];
    var emailid = req.query['emailid'];
    var bparam = req.query['bparam'];
    console.log ('User entered ' + bname + ' ' + fname + ' ' + emailid + ' ' + bparam);
};

2 个答案:

答案 0 :(得分:0)

为什么不检查“req.query”&#39;对象

的console.log(REQ);

答案 1 :(得分:0)

您可以使用multer来处理文件上传。结合身体解析器。

在multer完成其任务后,您在req.file中获取文件。