需要通过流将文件上传到cloudinary

时间:2019-01-29 11:01:23

标签: javascript typescript

我正在尝试将图像上传到Cloudinary,但是我不知道如何将上传的文件放入ReadStream中。我目前有以下代码:

export async function uploadImage(file: any, folder: string): Promise<string> {
    return new Promise<string>((resolve, reject) => {
        try {
            if (!file) {
                reject(new Error("No Image file"));
            }
            const options = {
                folder,
                public_id: utilities.generatePushID(),
            };
            const stream = cloudinary.uploader.upload_stream(options, (error: any, result: any) => {
                if (error) {
                    reject(new Error("Couldn't upload"));
                }
                resolve(result.public_id);
            });

            return fs.createReadStream(file.path).pipe(stream); // Error occurs here
        } catch (err) {
            reject(InternalError(err));
        }
    });
}

问题是file.path始终是undefinedfile来自使用req.file上传的multer。我该如何使ReadStream正常工作?

0 个答案:

没有答案