angular - 如何将字符串变量作为参数发送到 onload 函数
问题描述
我正在尝试将文件转换为 Base64 并将字符串存储到自变量中:
sellersPermitString: string;
DriversLicenseString: string;
InteriorPicString: string;
ExteriorPicString: string;
这是我正在使用的方法,它只允许我将 base64 保存到 imageSrc
imageSrc;
handleInputChange(files) {
var file = files;
var pattern = /image-*/;
var reader = new FileReader();
if (!file.type.match(pattern)) {
alert('invalid format');
return;
}
reader.onloadend = this._handleReaderLoaded.bind(this);
reader.readAsDataURL(file);
}
_handleReaderLoaded(e) {
let reader = e.target;
var base64result = reader.result.substr(reader.result.indexOf(',') + 1);
this.imageSrc = base64result;
console.log(this.imageSrc)
}
这就是我试图将base64的值保存到我的变量中的方式,但我没有得到第一个文件,只有其他文件。
public picked(event, field) {
let fileList: FileList = event.target.files;
if (fileList.length > 0) {
const file: File = fileList[0];
if (field == 1) {
this.sellersPermitFile = file;
this.handleInputChange(file); //turn into base64
this.sellersPermitString = this.imageSrc;
console.log(this.sellersPermitString)
}
else if (field == 2) {
this.DriversLicenseFile = file;
this.handleInputChange(file); //turn into base64
this.DriversLicenseString = this.imageSrc;
console.log(this.DriversLicenseString)
}
else if (field == 3) {
this.InteriorPicFile = file;
this.handleInputChange(file); //turn into base64
this.InteriorPicString = this.imageSrc;
console.log(this.InteriorPicString)
}
else if (field == 4) {
this.ExteriorPicFile = file;
this.handleInputChange(file); //turn into base64
this.ExteriorPicString = this.imageSrc;
console.log(this.ExteriorPicString)
}
}
else {
alert("No file selected");
}
}
有任何想法吗?
换句话说,我想要一个接收文件和变量来存储结果 base64 的方法。
解决方案
将图像转换为 base64 是异步操作,为什么文件未定义,因为这一行
this.sellersPermitString = this.imageSrc;
在第二次将图像转换为base64
问题之前执行
this.DriversLicenseString = this.imageSrc;
DriversLicenseString
将包含第一个文件的值。
设置值的安全位置是_handleReaderLoaded
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
imageSrc;
sellersPermitFile: any;
DriversLicenseFile: any;
InteriorPicFile: any;
ExteriorPicFile: any;
//base64s
sellersPermitString: string;
DriversLicenseString: string;
InteriorPicString: string;
ExteriorPicString: string;
//json
finalJson = {};
currentId: number = 0;
addPictures() {
this.finalJson = {
"sellersPermitFile": this.ExteriorPicString,
"DriversLicenseFile": this.DriversLicenseString,
"InteriorPicFile": this.InteriorPicString,
"ExteriorPicFile": this.ExteriorPicString
}
}
public picked(event, field) {
this.currentId = field;
let fileList: FileList = event.target.files;
if (fileList.length > 0) {
const file: File = fileList[0];
if (field == 1) {
this.sellersPermitFile = file;
this.handleInputChange(file); //turn into base64
}
else if (field == 2) {
this.DriversLicenseFile = file;
this.handleInputChange(file); //turn into base64
}
else if (field == 3) {
this.InteriorPicFile = file;
this.handleInputChange(file); //turn into base64
}
else if (field == 4) {
this.ExteriorPicFile = file;
this.handleInputChange(file); //turn into base64
}
}
else {
alert("No file selected");
}
}
handleInputChange(files) {
var file = files;
var pattern = /image-*/;
var reader = new FileReader();
if (!file.type.match(pattern)) {
alert('invalid format');
return;
}
reader.onloadend = this._handleReaderLoaded.bind(this);
reader.readAsDataURL(file);
}
_handleReaderLoaded(e) {
let reader = e.target;
var base64result = reader.result.substr(reader.result.indexOf(',') + 1);
//this.imageSrc = base64result;
let id = this.currentId;
switch (id) {
case 1:
this.sellersPermitString = base64result;
break;
case 2:
this.DriversLicenseString = base64result;
break;
case 3:
this.InteriorPicString = base64result;
break;
case 4:
this.ExteriorPicString = base64result;
break
}
this.log();
}
log() {
// for debug
console.log('1', this.sellersPermitString);
console.log('2', this.DriversLicenseString);
console.log('3', this.InteriorPicString);
console.log('4', this.ExteriorPicString);
}
}
推荐阅读
- reactjs - 如何使用 react 和 redux-thunk 在异步实体创建 (POST) 后刷新实体列表 (GET)
- python - 从 csv (Python) 读取数据的问题
- xaml - Xamarin.Forms Shell 在后面的代码中生成一个 Top Tapped Page
- unit-testing - (如何)您是否在单元测试中测试您的依赖项?
- raku - 为什么这两个做同样事情的脚本之间存在如此大的性能差异?
- powershell - Export PowerShell prompt to remote session
- php - 如何解决 Yii2 中的未知数据库错误?
- grails - Grails,GORM - 绑定 OneToMany 数据时没有脏属性
- python - Python 将 ctypes 库与 math.h 一起使用,但答案始终为 0
- angular - Twilio 视频:track.attach 不是函数