Ionic:undefined不是构造函数FileReader

时间:2017-11-19 22:39:07

标签: angular ionic-framework cordova-plugins

我在创建FileReader(来自@ ionic-native / file)实例时出现问题:

let f = new FileReader();

发生以下错误:

TypeError: undefined is not a constructor (evaluating 'new __WEBPACK_IMPORTED_MODULE_2__ionic_native_file__["FileReader"]()')

我不明白为什么!

我的配置是:

  • nodejs v8.9.1
  • npm:v5.5。&
  • 离子:3.9.3
  • angular:v5.0.1
  • iOS模拟器

有关更多信息,我使用此代码:

private readFile(file: any) {
     const reader = new FileReader();
     reader.onloadend = () => {
        const formData = new FormData();
        const imgBlob = new Blob([reader.result], {type: file.type});
        formData.append('file', imgBlob, file.name);
        this.postData(formData);
    };
    reader.readAsArrayBuffer(file);
 }

新FileReader()

发生错误

感谢。

2 个答案:

答案 0 :(得分:0)

我没有解决我的FileReader问题,但我使用了其他解决方案:

public constructor(private fileTransfer: FileTransfer) {
}


public uploadImage(url: string, imageUri: string, fileName: string): Promise<boolean> {
    return new Promise<boolean>((resolve, reject) => {
        const options: FileUploadOptions = {
            fileKey: 'file',
            fileName: fileName,
            httpMethod: 'POST'
        };

        const fileTransfer: FileTransferObject = this.fileTransfer.create();
        fileTransfer.upload(imageUri, url, options)
            .then(fileUploadResult => {
                console.log('saveImage', fileUploadResult.response);
                resolve(fileUploadResult.responseCode == 201);
            })
            .catch(error => {
                console.error('saveImage', error.code);
                reject(error);
            });
    });
}

imageUri来自:

addPicture() {
    const options: CameraOptions = {
        quality: 100,
        sourceType: PictureSourceType.PHOTOLIBRARY,
        destinationType: this.camera.DestinationType.FILE_URI,
        encodingType: this.camera.EncodingType.JPEG,
        mediaType: this.camera.MediaType.PICTURE
    };

    this.camera.getPicture(options)
        .then(imageUri => {
            console.log(imageUri);
            this.selectedPictureUriToSend = imageUri;
            this.selectedPictureUri = normalizeURL(imageUri);
        })
        .catch(error => {
            console.error(error);
        });
}

我调用预览功能:

public async publish() {
    try {
        if (this.selectedPictureUriToSend) {
            let fileName = this.selectedPictureUriToSend.substr(this.selectedPictureUriToSend.lastIndexOf('/') + 1);
            this.uploadImage('http://localhost:8181/mythreadsproject/api/image', this.selectedPictureUriToSend, fileName)
            .then(response => {
                console.log(response);
            })
            .catch(error => {
                console.error(error);
            });
        }
    } catch (error) {
        console.error(error);
    }
}

答案 1 :(得分:0)

Cordova polyfill将原始FileReader保存在FileReader._realReader中 您可以通过以下方式重新分配它:

let fr= new FileReader(); 
let rfr = fr._realReader;

FileReader = rfr.constructor;