uploadFileOpt: function (tempImagePath) {//上传图片
api.showLoading('上传中...')
let path = tempImagePath
console.log(path)
console.log(Url.config.apiUrl_upload+'attachment/file/upload')
var list = path.split('.')
wx.getFileSystemManager().readFile({
filePath: path, //选择图片返回的相对路径
encoding: 'binary', //编码格式
success: res => { //成功的回调
console.log(res)
console.log(res.data)
//这是正确的上传方式(在h5内)
// 不知道我展现的问题方式对不对,大佬看懂了吗?
console.log(encodeURI(res.data))
wx.request({
url: Url.config.apiUrl_upload+'attachment/file/upload', //仅为示例,并非真实的接口地址
header: {
'content-type': 'multipart/form-data; boundary=XXX', // 默认值
// "accept": "utf-8",
},
method:"POST",
data:'\r\n--XXX' +
'\r\nContent-Disposition: form-data; name="files"' +
'\r\n' +
'\r\n' + res.data +
'\r\n--XXX' +
'\r\nContent-Disposition: form-data; name="formats"' +
'\r\n' +
'\r\n'+ ['.'+list[list.length-1]] +
'\r\n--XXX--',
success (res) {
console.log(res)
}
})
}
})
},
你是不是弄得太麻烦了,获取到临时路径,直接UploadFile就行了
url: url,
filePath:path,
name: 'files',
formData: {
'formats': ['.'+list[list.length-1]]
},
success (res){
const data = res.data
//do something
}
})
//代码如上,有问题吗?不是这样写的吗