這爲我工作,但是這看起來很奇怪
if (isFile(input)) {
const f = new File([blob], input.name)
resolve(f as T)
} else if (isBlob(input)) {
resolve(blob as T)
}
from:
export
function resize<T extends Blob | File>(input: T, width: number, height: number): Promise<T> {
return new Promise((resolve, reject) => {
const image = new Image()
image.onload =() => {
const canvas = document.createElement('canvas')
canvas.width = width
canvas.height = height
pica.resize(image, canvas)
.then((result) => pica.toBlob(result, input.type, 85))
.then((blob: Blob) => {
if (isFile(input)) {
const f = new File([blob], input.name)
resolve(f as T)
} else if (isBlob(input)) {
resolve(blob as T)
}
})
.catch(err => reject(err))
}
image.src = URL.createObjectURL(input)
})
}
從來沒有過過,但不應該是'Promise .resolve'? –
Will
謝謝,它的工作原理是這樣的 返回Promise.resolve(new File([new Blob()],'test.jpg'))Promise 但我仍然不明白 –
Gah,我只是不知道足夠的打字稿的泛型(和文檔,在一個快速squizz,沒有足夠的信息 - 他們甚至沒有涵蓋'擴展文件| Blob'語法,這是我的頭從頭開始)。我有點震驚,因爲Promise'*作品*。怎麼回事,[Anders?](https://en.wikipedia.org/wiki/Anders_Hejlsberg)我想你會*使用'Promise .resolve',並且'Promise '會導致在一個空值被返回...你確定嗎? –
Will