cju*_*obi 12 javascript laravel vue.js
请在下面是我的vue组件的脚本部分中的代码.
我正在获取所有输入字段,但图像和视频上传显示空值.
我试图解决这个问题,但无济于事.
playVideo(url) {
let video = $('#video-preview').get(0);
video.preload = 'metadata';
// Load video in Safari / IE11
if (url) {
video.muted = false;
video.playsInline = true;
video.play();
}
},
// Read a file input as a data URL.
readDataUrl(input, callback) {
if (input.files && input.files[0]) {
let fileReader = new FileReader();
fileReader.onload = function () {
callback(fileReader.result);
};
fileReader.readAsDataURL(input.files[0]);
}
else {
callback(null);
}
},
// Read a file input as an object url.
readObjectUrl(input, callback) {
if (input.files && input.files[0]) {
let fileReader = new FileReader();
fileReader.onload = function () {
let blob = new Blob([fileReader.result], {type: input.files[0].type});
let url = URL.createObjectURL(blob);
callback(url, blob);
};
fileReader.readAsArrayBuffer(input.files[0]);
}
else {
callback(null);
}
},
}
}
Run Code Online (Sandbox Code Playgroud)
我想要实现的是上传图像和视频文件,在保存为blob之前预览它们.
上图显示了我的回复@samayo
图像和视频blob显示空值.
het*_*het 12
如果您想上传图像并在提交之前预览它,您可以选择使用简单且实用的方式。
<template>
<input type="file" accept="image/*" @change="onChange" />
<div id="preview">
<img v-if="item.imageUrl" :src="item.imageUrl" />
</div>
</template>
<script>
export default {
name: 'imageUpload',
data() {
return {
item:{
//...
image : null,
imageUrl: null
}
}
},
methods: {
onChange(e) {
const file = e.target.files[0]
this.image = file
this.item.imageUrl = URL.createObjectURL(file)
}
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
如果您使用axios
或fetch
上传vue文件非常简单.
这是我当前项目的副本/面食.我用axios上传图片:
首先,您需要让您的输入看起来像这样:
<input type="file" accept="image/*" @change="uploadImage($event)" id="file-input">
Run Code Online (Sandbox Code Playgroud)
然后添加如下方法:
methods: {
uploadImage(event) {
const URL = 'http://foobar.com/upload';
let data = new FormData();
data.append('name', 'my-picture');
data.append('file', event.target.files[0]);
let config = {
header : {
'Content-Type' : 'image/png'
}
}
axios.put(
URL,
data,
config
).then(
response => {
console.log('image upload response > ', response)
}
)
}
}
Run Code Online (Sandbox Code Playgroud)
I think in your case you are looking for a solution like this
example: uploading a image and previewing it before submission
<template>
<div>
<img src:"previewImage" class="uploading-image" />
<input type="file" accept="image/jpeg" @change=uploadImage>
</div>
</template>
<script>
export default {
name:'imageUpload',
data(){
return{
previewImage:null
}
},
methods:{
uploadImage(e){
const image = e.target.files[0];
const reader = new FileReader();
reader.readAsDataURL(image);
reader.onload = e =>{
this.previewImage = e.target.result;
console.log(this.previewImage);
};
}
}
} // missing closure added
</script>
<style>
.uploading-image{
display:flex;
}
</style>
Run Code Online (Sandbox Code Playgroud)
我是根据其他几个人的回答来总结一下的。
this.image
已进行 base64 编码并准备发送到您的 API。
<template>
<v-file-input
v-model="file"
chips
accept="image/*"
label="Image"
@change="onFileChange"
/>
</template>
<script>
export default {
data: { file: null, image: null },
methods: {
onFileChange() {
const reader = new FileReader()
reader.readAsDataURL(this.file)
reader.onload = e => {
this.image = e.target.result
console.log(this.image)
}
},
},
}
</script>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
15285 次 |
最近记录: |