中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
使用文件管理器选择文件并放到分布式目录
微信扫码分享
documentSelect(fileType: number): void { try { let DocumentSelectOptions = new picker.DocumentSelectOptions(); let documentPicker = new picker.DocumentViewPicker(); documentPicker.select(DocumentSelectOptions).then((DocumentSelectResult: Array<string>) => { for (let documentSelectResultElement of DocumentSelectResult) { let buf = new ArrayBuffer(CommonConstants.FILE_BUFFER_SIZE); let readSize = 0; let file = fileIo.openSync(documentSelectResultElement, fileIo.OpenMode.READ_ONLY); let readLen = fileIo.readSync(file.fd, buf, { offset: readSize }); // File name is not supported chinese name. let fileName = file.name; if (!fileName.endsWith(imageIndex[fileType].fileType) || new RegExp("\[\\u4E00-\\u9FA5]|[\\uFE30-\\uFFA0]", "gi").test(fileName)) { promptAction.showToast({ message: $r('app.string.alert_message_chinese') }) return; } let destination = fileIo.openSync(getContext() .filesDir + '/' + fileName, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); let destinationDistribute = fileIo.openSync(getContext() .distributedFilesDir + '/' + fileName, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); while (readLen > 0) { readSize += readLen; fileIo.writeSync(destination.fd, buf); fileIo.writeSync(destinationDistribute.fd, buf); console.info(destinationDistribute.path); readLen = fileIo.readSync(file.fd, buf, { offset: readSize }); } fileIo.closeSync(file); fileIo.closeSync(destination); fileIo.closeSync(destinationDistribute); this.appendix.push({ iconIndex: fileType, fileName: fileName }); } Logger.info(`DocumentViewPicker.select successfully, DocumentSelectResult uri: ${JSON.stringify(DocumentSelectResult)}`); }).catch((err: BusinessError) => { Logger.error(`DocumentViewPicker.select failed with err: ${JSON.stringify(err)}`); }); } catch (error) { let err: BusinessError = error as BusinessError; Logger.error(`DocumentViewPicker failed with err: ${JSON.stringify(err)}`); } }