Reputation:
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);
}}
i used readAsBinaryString to get file data but the problem is the browser console says readAsBinaryString' on 'FileReader': parameter 1 is not of type 'Blob'. any one please help to resolve
Upvotes: 0
Views: 151
Reputation:
Yeah, i agree with both. In my point of view use Synchronization method is not a good option because think
So better option you need to Do a front end validation for file size with what ever method you have choosen
Upvotes: 1
Reputation: 1288
The problem is javascript is asynchronous so in your code the api call hit before read the file fully. Settimeout is a one option to handle this issue but its not a recommended one because the file size is small your timeout 2s is okay. Think if one large file need to more than 2s to read the content...what will happen..that should be pass a empty value for read.result So you need to try a Synchronization method to handle like
var fs = require("fs");
fs.readFileSync(‘abc.txt’,function(err,data){
if(!err) {
console.log(data);
}
});
console.log("something else");
Before this you need to install file stream npm and input fs
Upvotes: 0
Reputation:
I assumed your api call happens before read the file completely. So you can easily resolved that by setting time out but you already done.
I think you missed assigned for this statement.Please try this code it's should work
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);
}
}
Upvotes: 0