How send SVG-element to NodeJS server - javascript

I need to send SVG-element to server and then rewrite SVG-file in folder. But i dont understand how to get element in request.body, its "undefined".
It's my fetch

Well, i find the answer.
You need to convert svg-element to base64, before send it to server.
On the server you need to create buffer from request.body and then convert it to string

Related

Trying to send a base64 encode image through a put request on an express server results in a request entity to large error

I am adding a feature to a site and basically the user uploads a photo, crops it to the correct size, and then the image is encoded with base64 and sent to a express web server. It does this trough a put request to update data that is already there. When I send it from the website, The express server gives me an error that the request entity is too large. I have tried putting the base64 string in put requests, url querys, and headers. None of them work. Could someone help me figure out what is going on and try and fix it.

Saving images(base64) to db (nodejs + postgres) and returning them to client

I want to save base64 (I already converted it on server side) to db. I successfully did it, but I want to return them to client side by GET request.
The problem is when I decode base64 format to File(I need to send to client side File, not base64).
The best solution I found is to use fs.writeFile function, that saves file to disk(nodejs folder). But the flow looks bad: By GET request I need to get base64 from DB,convert it to File, save File to nodejs folder, then send File to client side and then delete this File from nodejs folder.
Maybe anybody can optimize this solution without saving files to nodejs folder? Thanks :)

Why to use Blob at all if I can save file pathes in database and actual files in storage?

I know that blob is a data type for binary data as integer is a datatype for int. As they say, It's used to store files directly in database (we move our audio file into blob, and save that blob in database).
Question 1) why to store blob for audio if I can just put the audio in storage for example path /var/www/audio.mp3 and in database I store path_name /var/www/audio.mp3?
Question 2) which is better ? how netflix stores movies? just blobs or what?
Question 3) Curious if there're any cons or prons if you could just give me ideas so that I know when to use them .
Putting the blob in the database, rather than a file, allows you to grow to multiple servers with load balancing. If you put the data in files, you would have to replicate the files between the server. Most databases have built-in replication features, this isn't as easy for regular files.
Better to use external storage/cdn for serving such kind of large content.
How Netflix and our works? They upload content on external bucket i. e. S3 and write file name in db for identification. According to user file access frequency that file cache on CDN/edge location. User will get awesome experience while content server from their nearest edge location
With blob you can store all kinds of stuff.
Do you communicate with an API via SOAP or JSON and want to store it in the database? Use a blob. Want to log what a user filled into a form when it threw an exception? Store the entire post as a blob. You can save everything as is. It's handy for logging if you have different data formats. I know an API which expects some data via SOAP and some as JSON. To log the communication I use blob because the response may be in XML, JSON, a number (http code 203 for empty but accepted) or an exception as array.

How to send large data using get method?

I have to send an json array whixh length is above 2kb(limit of GET method=2kb) but when i use post method the file download dialog box is not appering as the file is download in working directory
and when i use get method download box is appered but pdf is null.Is anyone provide me the solution for this
in most of cases (few techno allow it like elasticsearch), we can not send a json request body in a get method. Why you don't use a Post? you can send and get the json you want.

How to send binary data back to client using GraphQL

I have a GraphQL server, hosted on express. I want to return images to the client by sending back nodejs buffer objects. How can i config graphql server, to return bytes, instead of json? I don't wish to do this through base64, as the image are large in size.
You have to return JSON, but there's still a way. We're using GraphQL to return images stored in Blob fields in a legacy sql database. We're using sequelize, graphql-sequelize, and graphql-js.
We've defined the Blob fields to be String types in our graphql schema and so they come through in the json reply just fine.
Then we convert to a buffer before delivering, like
const imgBuffer = new Buffer.from(imgObj.data, 'ascii');
The only problem is we're now having trouble saving image data back to the database via the graphql interface. Our mutation function gives us a syntax error when it finds certain bad unicode characters in the strings, like \U0000 and whatnot (so I found your question looking for a solution to that).
There's a way, but it's complicated, and very manual, and I'm only going to give you an overview of what I've done in ApolloServer, but I think it should be enough.
First, you need to use the "Accept" header in your request to send a binary mime type, and send a matching "Content-Type" in your response. This is nessisary to be efficient, but not nessisary to work, as you'll see (with EJSON).
To serialize and deserialize respecting the headers you may need to write an express middleware, and you'll need to handle base64 encoding with a {$data: "..."} encapsulating object (as EJSON does) or just (strangely) returning null, if someone makes a request for binary data using "application/json" for their "accept" header. You'll also want to choose what binary formats that you'll support. I only use 1: "application/x-msgpack", but I hear that "application/cbor" is becoming more popular. You can use a library for EJSON, MessagePack, and CBOR to do your serialization, so this isn't as hard as it sounds.
I would then strongly recommend using the #defer on any images. See this post for more information on #defer: https://www.apollographql.com/blog/introducing-defer-in-apollo-server-f6797c4e9d6e/
I've done it. It wasn't easy, and it would be better if ApolloServer worked this way "out of the box".
It's better to send a hashed & temporary link to download it
The URL can be hashed to be non-accessible by other users.
Backend can expire the link or remove the binary file on the static server.
There might be an answer to your question by using the node module found here.

Categories