通过multer创建ReadStream并将文件上传到远程服务器

时间:2020-09-16 14:37:14

标签: node.js express multer

所以我想将大文件上传到远程,所以我正在创建ReadStream

const stream = fs.createReadStream(req.file.path);

async uploadStream(stream: NodeJS.ReadableStream, filePath: string) {
        const dir = path.dirname(filePath);
        if (!(await this.exists(dir))) {
            await this.mkdir(dir);
        }

        await this.putStream(stream, filePath);
        return this.url(filePath);
    }



async putStream(stream: NodeJS.ReadableStream, filePath: string) {
        await this.connect();
        const normFIlePath = this.normalizePath(filePath);
        await this.sftp.put(stream, normFIlePath).catch(e => {
            console.log(e);
        });
        await this.disconnect()
    }

上传后,我正在将数据保存到数据库并重现文件url

file = await fileRepo.save(file);
res.send({
            id: file.data.itemId,
            src: file.url
        });

文件正在上传,但是之后返回错误:

(node:18) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

0 个答案:

没有答案
相关问题