js的文件读取onload函数没办法直接返回值,但是有很多时候我们又需要返回一个在处理后的值。实现方法是传递一个绑定了this的回调函数
const callback = (result)=>{
this.result = result
console.log(this.result)
}
file_name = this.selectFile.name;
if(!file_name.match(reg)){
console.error('the file chosed must be a image')
return
}
if (file_name) {
getTextFromImage(this.selectFile,callback.bind(this))
} else {
console.warn("the file not existed");
}
export function getTextFromImage(file,callback){
var reader = new FileReader()
reader.readAsArrayBuffer(file)
reader.onload = async function () {
var fileData = this.result
var appId = '726fc'
var secretCode = '1ea026885'
var url = 'https://api.textin.com/robot/v1.0/api/text_recognize_3d1'
let result = await axios.post(url,{data:fileData},{headers:{'x-ti-app-id':appId,'x-ti-secret-code': secretCode}})
callback(result)
}
}
|