getFiletoValidate = () => {
const fName = this.props.fileName;
const selectFile = this.props.selectedFile;
const inputValue = this.fileInput.value;
const providernameId = this.props.endL4;
const messsageTypeId = this.props.endType;
var read = new FileReader();
read.readAsBinaryString(selectFile);
if (inputValue === "") {
window.Notification.showWarning("Warning,Please choose a file to validate");
} else
{
setTimeout(function() {
api.messageValidator(fName, providernameId, messsageTypeId, read.result,this.handleFileSuccessResponse,this.handleFileFailResponse);
}, 2000);
}}
我使用readAsBinaryString来获取文件数据,但问题是浏览器控制台显示‘readAsBinaryString’on 'FileReader':参数1不是'Blob‘类型。任何人请帮助解决
发布于 2018-06-01 13:13:25
我假设您的api调用发生在完全读取文件之前。所以你可以通过设置超时来很容易的解决这个问题,但是你已经做到了。
我想你错过了这个statement.Please的赋值,试试这个代码吧,它应该可以工作
getFiletoValidate = () => {
const fName = this.props.fileName;
const selectFile = this.props.selectedFile;
const inputValue = this.fileInput.value;
const providernameId = this.props.endL4;
const messsageTypeId = this.props.endType;
var read = new FileReader();
read.readAsBinaryString(selectFile);
if (inputValue === "") {
window.Notification.showWarning("Warning,Please choose a file to validate");
}else
{
setTimeout(function() {
api.messageValidator(fName, providernameId, messsageTypeId,read.result,self.handleFileSuccessResponse,self.handleFileFailResponse);
}, 2000);
}
}
发布于 2018-06-01 13:23:14
问题是javascript是异步的,所以在你的代码中,api调用在完全读取文件之前命中。Settimeout是解决此问题的一个选项,但不推荐这样做,因为文件大小很小,超时2秒就可以了。考虑一下,如果一个大文件需要2秒以上的时间来读取content...what,那么happen..that应该为read.result传递一个空值,所以您需要尝试一个同步方法,如下所示
var fs = require("fs");
fs.readFileSync(‘abc.txt’,function(err,data){
if(!err) {
console.log(data);
}
});
console.log("something else");
在此之前,您需要安装文件流npm并输入fs
https://stackoverflow.com/questions/50596528
复制相似问题