JS关于多张图片上传显示报错不影响后面图片上传方法

关于多张图片上传或者下载显示报错后会程序会终止执行,从而影响后面图片上传。

解决方法:

/*能正常访问的图片*/
const url = 'https://2vimg.hitv.com/100/2308/0109/5359/dqKIZ7d4cnHL/81Vu0c.jpg?x-oss-process=image/format,webp';
/*不能正常下载的图片 (跨域问)*/
const urls = 'https://p26-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/36b749b8bc954816b90998beef6a93e5~noop.image?_iz=58558&from=article.pc_detail&x-expires=1691135481&x-signature=ohziADdEWZ1qrfRqUI6xIitOnVk%3D';

(function () {
    fetchFile(url, 'image');
    fetchFile(urls, 'images');
 
})()

async function fetchFile(url, fileName) {

    let promise = new Promise(async function (resolve, reject) {
        // try {
        await fetch(url).then((response) => response.blob())
            .then((blob) => {
                const file = new File([blob], fileName, {type: blob.type, lastModified: Date.now()})
                resolve(file);
            })
            .catch(error => reject(error))
        // } catch (e) {
        //     reject(e)
        // }
    })

    promise.then(
        /*请求成功*/
        function resolve(file) {
            const box = document.getElementsByClassName('box1')
            box[0].src = URL.createObjectURL(file);
            console.log(file)
        },
        /*请求失败报错*/
        function reject(error) {
            const box = document.getElementsByClassName('box')
            box[0].src = 'https://1vimg.hitv.com/100/2307/2810/1525/2WZw3cbDPP/bjgE99.jpg?x-oss-process=image/format,webp'
            console.log("报错设置默认图片")
            console.log(error)
        }
    )
}
 

你可能感兴趣的:(javascript,开发语言,ecmascript,报错)