Cristyan
Cristyan

Reputation: 670

Upload JSON data as a file

I have an object (data), and I need to convert it to JSON and upload it to a CDN. I plan to use JSON.stringify() and pass it the javascript object

It works perfectly upload files to the CDN from the browser, I wonder how I can emulate a FormData

The code I use to upload a file to the CDN is: (As an example)

const data = new FormData();
data.append('signature', auth.signature);
data.append('key', auth.id);
data.append('policy', auth.policy);
data.append('GoogleAccessId', auth.serviceAccount);
data.append('bucket', 'assets-visualive');
data.append('file', file);

Upvotes: 3

Views: 2213

Answers (2)

Cristyan
Cristyan

Reputation: 670

After much research the solution was in Using FormData Objects in MDN

Convert the object to JSON with JSON.stringify(), then create a blob of data and upload it as a file

const object = { key: 'data', n: 10 };
const json = JSON.stringify(object);
const blob = new Blob([json], { type: 'text/json' });

const data = new FormData();
data.append('file', blob);

Upvotes: 7

dziku
dziku

Reputation: 64

How do you make your ajax call? Be sure to include "processData : false" option.

Upvotes: -1

Related Questions