-
我想把用户选择的本地文件写入到手机中,如果直接将 file.stream()传入到sync.write参数中,手机中只会得到一个空文件,我应该怎么做呢 const fileHandles = await showOpenFilePicker({ multiple: true })
const adb = getAdb()
const sync = await adb?.sync()
if (!sync) return
for (let i = 0; i < fileHandles.length; i++) {
const fileHandle = fileHandles[i]
const file = await fileHandle.getFile()
const stream = file.stream()
await sync.write({ filename: `${pathStr.value}/${fileHandle.name}`, file: stream })
// 类型“ReadableStream<Uint8Array>”缺少类型“ReadableStream<Consumable<Uint8Array>>”中的以下属性: values, [Symbol.asyncIterator]ts(2739)
} |
Beta Was this translation helpful? Give feedback.
Answered by
gws0920
Apr 15, 2024
Replies: 1 comment 2 replies
-
我在这里找到了参考 const fileHandles = await showOpenFilePicker({ multiple: true })
const adb = getAdb()
const sync = await adb?.sync()
if (!sync) return
for (let i = 0; i < fileHandles.length; i++) {
const fileHandle = fileHandles[i]
const file = await fileHandle.getFile()
const stream = new WrapReadableStream<Uint8Array>(
file.stream() as unknown as ReadableStream<Uint8Array>
)
.pipeThrough(new WrapConsumableStream())
.pipeThrough(new ProgressStream((v) => {
console.log('progress: ', +(v / file.size * 100).toFixed(1) + '%')
}))
await sync.write({ filename: `${pathStr.value}/${fileHandle.name}`, file: stream })
} export class ProgressStream extends InspectStream<Consumable<Uint8Array>> {
public constructor (onProgress: (value: number) => void) {
let progress = 0
super((chunk) => {
progress += chunk.value.byteLength
onProgress(progress)
})
}
} |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
gws0920
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
我在这里找到了参考