我正在尝试将图像上传到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
始终是undefined
。 file
来自使用req.file
上传的multer
。我该如何使ReadStream
正常工作?