uploadDocmentFile.ets
3.8 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { picker } from '@kit.CoreFileKit';
import { common } from '@kit.AbilityKit';
import { request,BusinessError } from '@kit.BasicServicesKit';
import fs from '@ohos.file.fs';
export async function selectDocFile(){
const documentSelectOptions = new picker.DocumentSelectOptions();
// 选择文档的最大数目(可选)
documentSelectOptions.maxSelectNumber = 1;
// 选择文件的后缀类型['后缀类型描述|后缀类型'](可选) 若选择项存在多个后缀名,则每一个后缀名之间用英文逗号进行分隔(可选),后缀类型名不能超过100,选择所有文件:'所有文件(*.*)|.*';
documentSelectOptions.fileSuffixFilters = ['.png,.jpg,.jpeg', '.pdf'];
let context = getContext() as common.Context; // 请确保 getContext(this) 返回结果为 UIAbilityContext
// 创建文件选择器实例
const documentViewPicker = new picker.DocumentViewPicker(context);
try {
let documentSelectResult = await documentViewPicker.select(documentSelectOptions)
console.log('结果为:' + documentSelectResult)
return documentSelectResult
}catch (error){
let err: BusinessError = error as BusinessError;
console.error(`Invoke uploadFile failed, code is ${err.code}, message is ${err.message}`);
return error
}
}
// 拷贝图片路径到缓存
export async function copyCachePath(systemPhotoImagePath: string) {
let cacheDir = getContext().cacheDir // 获取缓存目录
// let endType = systemPhotoImagePath.split('.')
// let index = endType.length - 1
let filetype = 'pdf' // 设置图片类型
let filename = Date.now() + '.' + filetype // 设置图片名称
let fullPath = cacheDir + '/' + filename // 设置图片路径
console.log('文件路径为:' + fullPath)
const file = fs.openSync(systemPhotoImagePath, fs.OpenMode.READ_ONLY) // 打开图片
console.log('文件路径为:' + file)
fs.copyFileSync(file.fd, fullPath) // 复制图片
return [fullPath, filename, filetype]
}
export async function uploadDocFile() {
const documentViewPicker = new picker.DocumentViewPicker(getContext());
let documentSelectOptions = new picker.DocumentSelectOptions();
documentSelectOptions.maxSelectNumber = 1
documentSelectOptions.fileSuffixFilters = ['.png', '.pdf', '.jpg', '.jpeg'];
//选择是否对指定文件或目录授权,true为授权,当为true时,defaultFilePathUri为必选参数,拉起文管授权界面;false为非授权,拉起常规文管界面(可选)
documentSelectOptions.authMode = true;
let documentSelectResult: Array<string> = await documentViewPicker.select(documentSelectOptions)
// 文件上传的时候一定要先拷贝一份,然后在进行上传,要不然没权限操作文件
const context = getContext()
const endType = documentSelectResult[0].split('.')
const index = endType.length - 1
const fileType = endType[index]
// (以时间戳)生成一个新的文件名
const fileName = Date.now() + '.' + fileType
// 通过缓存路径+文件名 拼接出完整的路径
const copyFilePath = context.cacheDir + '/' + fileName
// 将文件 拷贝到 临时目录
const file = fs.openSync(documentSelectResult[0], fs.OpenMode.READ_ONLY)
fs.copyFileSync(file.fd, copyFilePath)
let uploadConfig: request.UploadConfig = {
url: 'http://xfwbzshd.crgx.net/common/upload', // 需要手动将 url 替换为真实服务器的 HTTP 协议地址
header:{
'Content-Type': 'multipart/form-data',
},
method: "POST",
files: [{ filename: fileName, name: "file", uri: `internal://cache/${fileName}`, type: "pdf" }],
data: [],
};
try {
let uploader = await request.uploadFile(getContext(), uploadConfig)
return uploader
} catch (err) {
console.error(`Failed to request the upload. err: ${JSON.stringify(err)}`);
return err
}
}