user9842241
user9842241

Reputation:

browser console says readAsBinaryString' on 'FileReader': parameter 1 is not of type 'Blob'

 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

Answers (3)

user9885735
user9885735

Reputation:

Yeah, i agree with both. In my point of view use Synchronization method is not a good option because think

  1. If your input file is broken you code stuck inside that file reader code snap that's why your page is broken without any issue
  2. If some one input a large file like 2GB something you have wait that much of time.It's not a good option because your performance is bad

So better option you need to Do a front end validation for file size with what ever method you have choosen

Upvotes: 1

MK Vimalan
MK Vimalan

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

user9879250
user9879250

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

Related Questions