前言:前端使用uniapp中的uni.canvasToTempFilePath方法将画板中的内容保存为jpg上传至后端处理
uni.canvasToTempFilePath({
canvasId: 'firstCanvas',
sourceType: ['album'],
fileType: "jpg",
success: function (res1) {
let signature_base64 = res1.tempFilePath;
let signature_file = that.base64toFile(signature_base64);
// 将签名存储到服务器
uni.uploadFile({
url: "convertJpgToPdf",
name: "file",
file: signature_file,
formData: {
busiId: 'ceshi12',
token: token
},
success: function (res1) {
console.log(res1);
}
})
}
});
// 将base64转为file
base64toFile: function(base64String) {
// 从base64字符串中解析文件类型
var mimeType = base64String.match(/^data:(.*);base64,/)[1];
// 生成随机文件名
var randomName = Math.random().toString(36).substring(7);
var filename = randomName + '.' + mimeType.split('/')[1];
let arr = base64String.split(",");
let bstr = atob(arr[1]);
let n = bstr.length;
let u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
return new File([u8arr], filename, { type: mimeType });
},
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
java代码:
首先在pom.xml中安装依赖
org.apache.pdfbox
pdfbox
2.0.8