[英]Preview Multiple Images Vue JS with background image css style
I'm trying to display a preview of some images when a user selects them before he uploads them, but I'm new to vue and I can't figure out how to apply the background image url style to each div (it must be with background image), here is my UploadComponent so far:当用户在上传之前选择它们时,我试图显示一些图像的预览,但我是 vue 新手,我不知道如何将背景图像 url 样式应用于每个 div(它必须是带背景图片),到目前为止,这是我的 UploadComponent:
<template>
<div class="content">
<h1>{{ title }}</h1>
<div class="btn-container">
<label class="button" for="images">Select Images</label>
<input type="file" id="images" name="images[]" multiple="multiple" @change="selectFiles"/>
</div>
<br><br>
<div class="block">
<h3>Selected images</h3>
<div v-for="image in images" v-bind:key="image" class="image-result" v-bind:style="{ backgroundImage: 'url(' + image + ')' }">
</div>
</div>
<div class="btn-container">
<button type="button" class="submit-btn" v-on:click="uploadImages">Submit Images</button>
</div>
<br><br>
<div class="block">
<h3>Uploaded images</h3>
<div class="files-preview">
</div>
</div>
</div>
</template>
<script>
import axios from 'axios';
export default {
name: 'UploadComponent',
data: () => ({
title: 'Select your images first and then, submit them.',
images: []
}),
methods:{
selectFiles(event){
var selectedFiles = event.target.files;
var i = 0;
for (i = 0; i < selectedFiles.length; i++){
this.images.push(selectedFiles[i]);
}
for (i = 0; i < this.images.length; i++){
let reader = new FileReader(); //instantiate a new file reader
reader.addEventListener('load', function(){
console.log(this);
}.bind(this), false); //add event listener
reader.readAsDataURL(this.images[i]);
}
},
uploadImages(){
const data = new FormData();
data.append('photos', this.images);
axios.post('url')
.then(res => {
console.log(res);
});
}
}
}
</script>
The divs are being created but I'm missing the part where I assign the background image, how can I solve this?正在创建 div,但我缺少分配背景图像的部分,我该如何解决这个问题?
You can't just put a File
directly as the parameter for background-image
.您不能只将File
直接作为background-image
的参数。 You started using FileReader
but didn't do anything with the result.您开始使用FileReader
但没有对结果做任何事情。
Here's what I would do below, I converted your images
array into a list of objects that have a file
property and a preview
property, just to keep the file and preview linked together.这是我在下面要做的,我将您的images
数组转换为具有file
属性和preview
属性的对象列表,只是为了将文件和预览链接在一起。
Vue.config.productionTip = false; Vue.config.devtools = false; new Vue({ el: "#app", data: () => { return { title: 'Select your images first and then, submit them.', images: [] } }, methods: { selectFiles(event) { var selectedFiles = event.target.files; for (var i = 0; i < selectedFiles.length; i++) { let img = { file: selectedFiles[i], preview: null }; let reader = new FileReader(); reader.addEventListener('load', () => { img.preview = reader.result; this.images.push(img); }); reader.readAsDataURL(selectedFiles[i]); } }, uploadImages() { const data = new FormData(); data.append('photos', this.images.map(image => image.file)); axios.post('url').then(res => { console.log(res); }); } } });
.image-result { width: 100px; height: 100px; background-size: contain; background-repeat: no-repeat; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div id="app" class="content"> <h1>{{ title }}</h1> <div class="btn-container"> <label class="button" for="images">Select Images</label> <input type="file" id="images" name="images[]" multiple="multiple" @change="selectFiles" /> </div> <br><br> <div class="block"> <h3>Selected images</h3> <div id="target-photos"> </div> <div v-for="image in images" class="image-result" v-bind:style="{ backgroundImage: 'url(' + image.preview + ')' }"> </div> </div> <div class="btn-container"> <button type="button" class="submit-btn" v-on:click="uploadImages">Submit Images</button> </div> <br><br> <div class="block"> <h3>Uploaded images</h3> <div class="files-preview"> </div> </div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.