使用ng2-file-upload在上传之前将图像转换为Base64

时间:2019-01-27 09:23:46

标签: angular file-upload base64 ng2-file-upload

当前,我正在使用 ng2-file-upload 库(https://www.npmjs.com/package/ng2-file-upload)进行角度上传,将文件上传到服务器,但是我有点费力地将数据发送到带有base64字符串的主体,请让我知道我在这里做错了什么

我的主要任务是确保正文中具有任意数量的二进制文件

下面是我尝试过的

public uploader: FileUploader = new FileUploader({
    url: 'http://test.com',
    itemAlias: 'photo',
    authToken: 'Bearer ' + this.accessToken,
    headers: [{ name: 'Content-Type', value: 'application/json; charset=utf-8' }],
    disableMultipart: true,
    formatDataFunctionIsAsync: true,
    formatDataFunction: async (item) => {
        return new Promise((resolve, reject) => {
            resolve({
                documentType: 'Photo',
                contentType: 'PNG',
                documentCode: '1234',
                uploadCode: '564',
                data: this.extractBinary(this.uploader.queue[0])
            });
        });
    }
});

extractBinary(file) {
    file.withCredentials = false;
    const reader = new FileReader();
    let binaryValue;
    reader.onload = (event) => {
        binaryValue = reader.result;
        return binaryValue;
    };
    return reader.readAsDataURL(file._file);
}

0 个答案:

没有答案