微信小程序文件上传服务器代码,微信小程序文件上传 Java(附代码)

微信小程序+Springboot实现文件上传

微信小程序代码

API接口:

使用方法:

wx.chooseMessageFile({

count:10,

type:'image',

success (res) {//tempFilePath可以作为img标签的src属性显示图片

const tempFilePaths =res.tempFiles

}

})

使用方法:

wx.chooseImage({

success (res) {

const tempFilePaths=res.tempFilePaths

wx.uploadFile({

url:'https://example.weixin.qq.com/upload', //仅为示例,非真实的接口地址

filePath: tempFilePaths[0],

name:'file',

formData: {'user': 'test'},

success (res){

const data=res.data//do something

}

})

}

})

upload.wxml

1

2 上传图片

3

4

5 上传文件

6

upload.js

upImage: function() {

wx.chooseImage({

count:1,

sizeType: ['original', 'compressed'],

sourceType: ['album', 'camera'],

success:function(res) {

const tempFilePaths=res.tempFilePaths

wx.uploadFile({

url:'http://localhost/picture',

filePath: tempFilePaths[0],

name:'file',

success(res) {

console.log("success")

console.log(res)

},

fail(res) {

console.log("失败")

console.log(res)

}

})

},

})

},

upFile:function() {

wx.chooseMessageFile({

count:1,

type:'file',

success(res) {

const tempFilePaths=res.tempFiles

wx.uploadFile({

url:'http://localhost/picture',

filePath: tempFilePaths[0].path,

name:'file',

success(res) {

console.log("success")

console.log(res)

},

fail(res) {

console.log("失败")

console.log(res)

}

})

}

})

},

Java代码

upload.java

@RequestMapping(value = "/picture", method =RequestMethod.POST)public String uploadPicture(HttpServletRequest request, HttpServletResponse response, @RequestParam("file") MultipartFile files) throwsException {try{

System.out.println("图片上传请求成功");

String type = files.getOriginalFilename().substring(files.getOriginalFilename().lastIndexOf("."));String path= "E:/demo/image/upload";

File fileParent = newFile(path);if (!fileParent.exists()) {

fileParent.mkdirs();

}

File targetFile= new File(path + "/", files.getOriginalFilename());if (!targetFile.exists()) {

targetFile.createNewFile();

}

files.transferTo(targetFile);returnfiles.getOriginalFilename();

}catch(Exception e) {return "上传失败";

}

}

服务器配置注意项

如果抛出以下异常请检查域名配置是否正确

如果域名配置正确并且 wx.uploadFile 中 url 参数准确无误尝试以下办法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值