Saturday, 19 September 2020

Create ReadStream And Uploade Files to a Remote Server via multer

So I Want Upload Large File To Remote,So I Am Creating 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()
    }

After Upload I Am Saving data to database and returing file url

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

File is Uploading But After That it's return errors:

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


from Create ReadStream And Uploade Files to a Remote Server via multer

No comments:

Post a Comment