-
次のコードをコピーして xxx.js ファイルにカプセル化し、プロジェクト フォルダー utiles の下に配置します。
export default function readImg (file) { return new Promise((resolve, reject) => { const img = new Image() const reader = new FileReader() reader.onload = function (e) { img.src = e.target.result } reader.onerror = function (e) { reject(e) } reader.readAsDataURL(file) img.onload = function () { resolve(img) } img.onerror = function (e) { reject(e) } }) }
-
使用方法: xxx.js ファイルをインポートし、メソッドを呼び出します
// 注意引入路径不要出错 import readImg from '@/utils/xxx.js' readImg(file).then(res => { // 输出图片img对象 console.log(res) }).catch(err => { console.log(err) })
jsはファイルfileオブジェクトをimgオブジェクトに変換します
おすすめ
転載: blog.csdn.net/lhh_gxx/article/details/128377119
おすすめ
ランキング