express学习过程中问题4 上传文件问题

express 3 之后

app.use(express.bodyParser());

// is equivalent to:
app.use(express.json());
app.use(express.urlencoded());
app.use(express.multipart());

貌似目的是为了让我们选择可以解析的内容,不需要上传这种模式是就只用

app.use(express.json());
app.use(express.urlencoded());
需要时在添加multipart
官网说明:http://expressjs.com/api.html#app.use

现在上传文件设这样设置

app.use(express.multipart({ keepExtensions: true, uploadDir: './public/images' }));

另外上传文件的表单如下

<form action="" class="form-horizontal" method="post" enctype='multipart/form-data'>
    <legend>上传文件</legend>
        <div class="form-group">
            <label for="exampleInputFile">File input</label>
            <input type="file" id="exampleInputFile" name="file">
        </div>
            <button class="btn btn-default" type="submit">
                上传
            </button>

</form>

其中input中必须要有name属性不然会报如下错误

http post以multipart/form-data形式上传的话name属性是必须的,request需要,如下

原文地址:https://www.cnblogs.com/ltchronus/p/3521275.html