[英]How to get file path from File object observer in v-file-input Vuetify?
我正在使用 Vuetify 構建功能上傳圖像。 在上傳到服務器之前我需要預覽圖像
在 v-input-file 中
<v-file-input
accept="image/*"
multiple
label="Add your files"
chips
filled
prepend-icon="mdi-camera"
@change="onAddFiles"
></v-file-input>
在方法改變
onAddFiles(files) {
console.log(files);
}
可以看到File object observer
數組中沒有信息file path
我不知道如何從此File object observer
獲取file path
。 請幫助我,我非常感激!
在數據中,包括以下內容
previews: [],
errorImage: "url of an image to use to indicate an error",
顯然你需要將errorImage
設置為有用的東西
然后在onAddFiles
中添加一些代碼來生成預覽數組
onAddFiles(files) {
this.previews = [];
files.forEach((file, index) => {
const reader = new FileReader();
reader.addEventListener('load', e => this.previews[index] = e.target.result);
reader.addEventListener('error', e => this.previews[index] = this.errorImage);
reader.readAsDataURL(file);
});
}
在您的標記中 - 無論您希望在哪里顯示預覽,例如
<span v-for="src in previews">
<img v-if="src && src.length" :src="src"/>
</span>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.