Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calling FileWrite.write returns Type Error #636

Open
canhduckhong opened this issue Sep 20, 2024 · 2 comments
Open

Calling FileWrite.write returns Type Error #636

canhduckhong opened this issue Sep 20, 2024 · 2 comments

Comments

@canhduckhong
Copy link

canhduckhong commented Sep 20, 2024

Bug Report

Problem

I got this error when trying to write a binary string into file.

this._realReader.readAsArrayBuffer is not a function. 
(In 'this._realReader.readAsArrayBuffer(file)', 
'this._realReader.readAsArrayBuffer' is undefined)

The error is thrown at FileReader.js, line 302.

FileReader.prototype.readAsArrayBuffer = function (file) {
    if (initRead(this, file)) {
        return this._realReader.readAsArrayBuffer(file);
    }
    const totalSize = file.end - file.start;
    readSuccessCallback.bind(this)('readAsArrayBuffer', null, file.start, totalSize, function (r) {
        const resultArray = (this._progress === 0 ? new Uint8Array(totalSize) : new Uint8Array(this._result));
        resultArray.set(new Uint8Array(r), this._progress);
        this._result = resultArray.buffer;
    }.bind(this));
}; 

Here is how I call the FileWriter in my code:

writeBlobToFile = (file: FileEntry, blob): angular.IPromise<void> => {
        return this.$q((resolve, reject) => {
            file.createWriter((fileWriter) => {
                fileWriter.write(blob);
                fileWriter.onwriteend = resolve;
                fileWriter.onerror = reject;
            }, reject);
     });
};

Environment, Platform, Device

iOS 17.6 and iOS 18.0
iPhone 13 Plus

Version information

cordova-plugin-file version 8.1.0
cordova-ios version 7.1.0

@breautek
Copy link
Contributor

It would be interesting to see in your environment what is being returned by:

const origFileReader = modulemapper.getOriginalSymbol(window, 'FileReader');

Because the only way

this._realReader.readAsArrayBuffer is not a function.
(In 'this._realReader.readAsArrayBuffer(file)',
'this._realReader.readAsArrayBuffer' is undefined)

makes sense is if

this._realReader = origFileReader ? new origFileReader() : {}; // eslint-disable-line new-cap

uses the false condition and assigns _realReader to {} which obviously has no methods and would explain the "readAsArrayBuffer is not a function ... readAsArrayBuffer is undefined"

@canhduckhong
Copy link
Author

Unfortunately I cannot reproduce the issue in my local environment. It is on a user device.
It is obvious
modulemapper.getOriginalSymbol(window, 'FileReader');
returns a falsy value.
It can be due to the modulemapper cannot find FileReader in the window object.
I wonder if we can have a fallback strategy for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants