这为我工作,但是这看起来很奇怪
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