wx.uploadFile({
url: “域名”,
filePath: that.data.imageList[0],
name: ‘file’,
header: { “Content-Type”: “multipart/form-data” },
// 设置请求的 header
success: function (res) {
console.log(res);
if (res.statusCode == 200 && !res.data.result_code) {
typeof success == “function” && success(res.data);
} else {
typeof fail == “function” && fail(res);
}
},
fail: function (res) {
console.log(res);
typeof fail == “function” && fail(res);
}
})
后端:nodejs
router.post(’/’, function(req, res, next) {
var form = new multiparty.Form();//新建表单
//设置编辑
form.encoding = ‘utf-8’;
//设置图片存储路径
form.uploadDir = “Uploads/gaoxiao/”;
form.keepExtensions = true; //保留后缀
form.maxFieldsSize = 2*1024*1024; //内存大小
form.maxFilesSize= 5*1024*1024;//文件字节大小限制,超出会报错err
//表单解析
form.parse(req, function(err,fields,files) {
console.log(req);
//报错处理
if(err){
console.log(err);
var u={“error” :1,“message”:‘请上传5M以图片’};
res.end(JSON.stringify(u));
return false;
}
//获取路径
var oldpath=files.imgFile[0][‘path’];
//文件后缀处理格式
if(oldpath.indexOf(’.jpg’)>=0){
var suffix=’.jpg’;
}else if(oldpath.indexOf(’.png’)>=0){
var suffix=’.png’;
}else if(oldpath.indexOf(’.gif’)>=0){
var suffix=’.gif’;
}else{
var u={“error” :1,“message”:‘请上传正确格式’};
res.end(JSON.stringify(u));
return false;
}
var url=‘Uploads/gaoxiao/’+Date.now()+suffix;
var fs=require(‘fs’);
//给图片修改名称
fs.renameSync(oldpath,url);
var u={ “error” : 0, “url” : ‘/’+url}
res.end(JSON.stringify(u));
});
});