【问题背景】
react组件form上传文件到不可见iframe,后端node express提示req.files未定义。
【问题解决】
在html页面上写入相同的表单如下,可以在后台获取文件
<form action="/file_upload" method="post" enctype="multipart/form-data" target="hidden-iframe">
<input type="file" name="image" size="50" />
<input type="submit" value="上传文件" id="submit-button"/>
</form>
查看渲染出的页面发现写在react组件中的form的enctype属性没有渲染出。
查询文档,该属性在react组件中的写法为encType,T大写。修改如下:
var TestComponent = React.createClass({
render: function() {
return (<div>
<form action="/file_upload" method="post" encType="multipart/form-data" target="hidden-iframe">
<input type="file" name="image" size="50" />
<input type="submit" value="上传文件" id="submit-button"/>
</form>
</div>
);
}
});
可以正常上传