内容简介:使用了 el-upload 的插件,但是一直都是报 404 ,在这个过程中,请注意您使用的 request header 中的 content-type 不同的类型需要的 NodeJS 的 request 解析方式不一样。在 Node 服务端接到用户的数据之后,就需要使用 fs 模块把图片解析成可以保存的文件流,存入磁盘中或者调用后台接口保存。调试了一个小时之后成功通过。把代码留下来,方便日后进行查询。
使用了 el-upload 的插件,但是一直都是报 404 ,在这个过程中,请注意您使用的 request header 中的 content-type 不同的类型需要的 NodeJS 的 request 解析方式不一样。在 Node 服务端接到用户的数据之后,就需要使用 fs 模块把图片解析成可以保存的文件流,存入磁盘中或者调用后台接口保存。调试了一个小时之后成功通过。把代码留下来,方便日后进行查询。
Vue 层
-------------------------------------
<el-upload class="avatar-uploader" accept="image/jpeg" action="agency/uploadPicForAgency" :data="{agencyId:'1',picFlag:'1'}" :multiple="false" :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeBackgroundAvatarUpload">
<img v-if="backgroundUrl" :src="backgroundUrl" width="100%" height="100%" class="avatar">
<i v-else="" class="el-icon-plus avatar-uploader-icon"></i>
</el-upload>
express 层
-----------------------------------
/**
* @api {post} agency/uploadPicForAgency 代理商图片上传接口,返回图片存入服务器的URL
* @apiName agency/uploadPicForAgency
* @apiGroup agency
*
* @apiParam {FileObject} imgInfo 图片文件对象
* @apiParam {String} agencyId 代理商ID
* @apiParam {String} picFlag 图片标记(1-登录页logo图,2-登录页背景图,3-内容页logo图)
*
* @apiParamExample {type} Request-Example:
* 传统的FORM表单提交
* Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryzJ4Ys0x4h70Z8heR
*
* ------WebKitFormBoundaryzJ4Ys0x4h70Z8heR
Content-Disposition: form-data; name="agencyId"
000192
------WebKitFormBoundaryzJ4Ys0x4h70Z8heR
Content-Disposition: form-data; name="picFlag"
1
------WebKitFormBoundaryzJ4Ys0x4h70Z8heR
Content-Disposition: form-data; name="file"; filename="Intel-logo.jpg"
Content-Type: image/jpeg
------WebKitFormBoundaryzJ4Ys0x4h70Z8heR--
*
* @apiSuccessExample {Object} Success-Response:
* {
* message: ""
* success: true
* data: {
* imageStr: "/9j/4AAQSkZJRgABAgAAZABkAAD/7AARRHVja3kAAQAEAAAAXA.............."
uploadPath: "agency/2019/05/54e4c76df1db41bbbca80c9a7ef16c5f.jpg"
}
* }
*/
router.post('/uploadPicForAgency', function (req, res, next) {
const methodName = 'MAgencyPlatformDataAPI.uploadPicForAgency';
var form = new formidable.IncomingForm();
form.encoding = 'utf-8'; //设置编辑
form.keepExtensions = true; //保留后缀
form.maxFieldsSize = 2 * 1024 * 1024; //设置单文件大小限制
form.parse(req, function (err, fields, files) {
const agencyId = fields.agencyId;
const picFlag = fields.picFlag;
var imageBuf = fs.readFileSync(files.file.path);
var imagebase = imageBuf.toString("base64");
var imgInfo = {
imgName: files.file.name,
imgStr: imagebase,
}
util.JSONRPC(methodName, [imgInfo,agencyId,picFlag], function (data) {
res.json({
success: true,
data: {
uploadPath: data,
imageStr: imagebase
},
message: ''
})
}, res);
})
});
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- axios上传图片,koa2接收保存上传的图片,lrz在上传前压缩图片
- SpringMVC教程--图片上传
- 前端图片压缩及上传
- 前端实现图片压缩上传
- 从FormData到图片上传
- php+WebUploader图片批量上传
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Remote
Jason Fried、David Heinemeier Hansson / Crown Business / 2013-10-29 / CAD 26.95
The “work from home” phenomenon is thoroughly explored in this illuminating new book from bestselling 37signals founders Fried and Hansson, who point to the surging trend of employees working from hom......一起来看看 《Remote》 这本书的介绍吧!