javascript - VueJs 实现图像预览并返回不正确的 url
问题描述
在vuejs
我想从用户选择的图像中进行图像预览,以下代码工作正常,我可以选择图像,但不幸的是,在执行此操作后reader.readAsDataURL(this.avatar);
,我返回了不正确的 url,http://localhost/fa/panel/true
并且我404
在console
.
我显示此表单的当前 url 是框架http://localhost/fa/panel/user-profile
支持的。Laravel
html
图像和input
标签
<img v-bind:src="image_preview" width="120" height="120" v-show="show_preview"/>
<input type="file" class="file-styled" name="avatar_path" @change="onFileChange">
vuejs
代码:
import {mapGetters, mapMutations} from "vuex";
export default {
data() {
return {
avatar: null,
image_preview: null,
show_preview: false,
}
},
methods: {
onFileChange(event) {
this.avatar = event.target.files[0];
let reader = new FileReader();
reader.addEventListener("load", function () {
console.log(reader.result) // return base-64 of image
this.image_preview = true;
this.show_preview = reader.result;
}.bind(this), false);
if (this.avatar) {
if (/\.(jpe?g|png)$/i.test(this.avatar.name)) {
console.log(this.avatar); // result is object image
reader.readAsDataURL(this.avatar);
}
}
}
}
};
解决方案
推荐阅读
- windows - NW.js Windows 10 任务栏图标始终显示为默认值
- java - 计算给定年份的平均价格
- python - 这是我应该如何在 discord.py 中使用“on_guild_join”吗?
- java - 制作定制的 guacamole Web 应用程序所需的配置
- ionic-framework - 我的 Visual Studio Code Intellisense 根本不起作用(Ionic/Angular 项目)
- ide - WebStorm 在编辑时保存文件
- c# - C# web 抓取没有结果
- ruby - 如何确定 Ruby 中值的数据类型是什么?
- youtube - YouTube Data API v3 中未显示直播活动
- sql-server - SQL Server 存储过程 oDate 作为参数无法识别