E
E
ezhebrikov2022-03-13 10:48:40
JavaScript
ezhebrikov, 2022-03-13 10:48:40

Why does a written file from base64/Buffer fail?

I get the base64 representation of the file. I'm trying to write to a file. It can be pdf/docx/jpeg, whatever.

The recording is normal, but when you try to open it, an error appears that the file may be damaged or incorrectly recorded.

const docBuffer = Buffer.from(content[1].trim(), 'base64');

    const stream = new Readable();

    const writeStream = createWriteStream(
        process.cwd()+'/file/'+name,
    );

    stream.push(docBuffer);
    stream.push(null);

    stream.pipe(writeStream);


Thank you for your help!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
lssssssssssl, 2022-03-13
@lsssssssssl

const filePath = path.join(process.cwd(), "file", name)

fs.writeFile(filePath, base64, "base64", (err) => {
            if (err) console.log(err)
        })

Try it like this - without extra code

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question