⼩程序⽂件上传服务器代码,⼩程序⽂件上传
Java(附代码)
⼩程序+Springboot实现⽂件上传
⼩程序代码
API接⼝:
使⽤⽅法:
wx.chooseMessageFile({
count:10,
type:'image',
success (res) {//tempFilePath可以作为img标签的src属性显⽰图⽚
const tempFilePaths =pFiles
}
})
使⽤⽅法:
wx.chooseImage({
success (res) {
const pFilePaths
wx.uploadFile({
url:'example.weixin.qq/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 pFilePaths wx.uploadFile({
url:'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 pFiles wx.uploadFile({
url:'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 = OriginalFilename().OriginalFilename().lastIndexOf("."));String path=
"E:/demo/image/upload";
File fileParent = newFile(path);if (!ists()) {
fileParent.mkdirs();
}
File targetFile= new File(path + "/", OriginalFilename());if (!ists()) {
}
}catch(Exception e) {return "上传失败";
}
}
服务器配置注意项
如果抛出以下异常请检查域名配置是否正确
如果域名配置正确并且 wx.uploadFile 中 url 参数准确⽆误尝试以下办法

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。