Back

[SOLVED]storage.createFile() creates image with MIME Type of "application/octet-stream"

  • 0
  • Storage
FUZZY
28 May, 2024, 08:21

I am working with node.js, avatarString is a base64 string of png looks like "data:image/png;base64,xxxxxxxx" The following code is a function instance that converts base64 to buffer to create an inputFile object and pass into createFile ` // src/index.js import InputFile from "./inputFile.js"; const fileName = userID + '.png'; const avatarString = params.avatar; const avatarBuffer = Buffer.from(avatarString, 'base64'); const fileResult = await storage.deleteFile('avatar', userID).then(() => { return storage.createFile('avatar', userID, InputFile.fromBuffer(avatarBuffer, fileName)); })

// src/inputFile.js export default class InputFile { stream; // class NodeJS.ReadableStream filename; // File name size; // Total final size of the file content

static fromBuffer = (buffer, filename) => { const stream = Readable.from(buffer); const size = Buffer.byteLength(buffer); return new InputFile(stream, filename, size); };

constructor(stream, filename, size) { this.stream = stream; this.filename = filename; this.size = size; } } ` After execution, a file of MIME Type: application/octet-stream was created instead of image/png

TL;DR
The issue where `storage.createFile()` creates an image with MIME Type of "application/octet-stream" has been solved. The solution involves removing the meta at the start of the base64 string using `params.avatar.replace(/^data:image\/\w+;base64,/, "")`.
FUZZY
28 May, 2024, 08:34

update: solved, looks like I need to get rid of the meta at start of the base64 string params.avatar.replace(/^data:image\/\w+;base64,/, "")

FUZZY
28 May, 2024, 08:34

[SOLVED]storage.createFile() creates image with MIME Type of "application/octet-stream"

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more