Angular - upload file as base64 - json

I am trying to upload files from Angular 4 app to a JSON API service that accepts base64 strings as file content.
So what I do is - read the file with FileReader.readAsDataURL, then when user confirms the upload I will create a JSON request to the API and send the base64 string of the file I got earlier.
This is where the problem starts - as soon as I do something with the "content" (log it, send it, w/e) the request will be send, but its insanely slow, e.g. 20 seconds for 2MB file.
I have tried:
using ArrayBuffer and manually converting it to base64
storing the base64 string in HTML and retrieving it later
reading the files after user clicks on upload button
using the old client from #angular/common
using plain XHR request
but everything leads to the same result.
I know where the problem lies. But why does it happen? Is it something browser specific or angular specific? Is there a more preferred approach (keep in mind it has to be base64 string)?
Notes:
changing anything in the API is beyond my control
API is fine, sending any file trough postman will finish immediately
Code:
This method runs when user adds file to the dropzone:
public onFileChange(files: File[]) : void {
files.forEach((file: File, index: number) => {
const reader = new FileReader;
// UploadedFile is just a simple model that contains filename, size, type and later base64 content
this.uploadedFiles[index] = new UploadedFile(file);
//region reader.onprogress
reader.onprogress = (event: ProgressEvent) => {
if (event.lengthComputable) {
this.uploadedFiles[index].updateProgress(
Math.round((event.loaded * 100) / event.total)
);
}
};
//endregion
//region reader.onloadend
reader.onloadend = (event: ProgressEvent) => {
const target: FileReader = <FileReader>event.target;
const content = target.result.split(',')[1];
this.uploadedFiles[index].contentLoaded(content);
};
//endregion
reader.readAsDataURL(file);
});
}
This method runs when users clicks save button
public upload(uploadedFiles: UploadedFile[]) : Observable<null> {
const body: object = {
files: uploadedFiles.map((uploadedFile) => {
return {
filename: uploadedFile.name,
// SLOWDOWN HAPPENS HERE
content: uploadedFile.content
};
})
};
return this.http.post('file', body)
}

For sending big files to server you should use FormData to be able to send it as multi-part instead of a single big file.
Something like:
// import {Http, RequestOptions} from '#angular/http';
uploadFileToUrl(files, uploadUrl): Promise<any> {
// Note that setting a content-type header
// for mutlipart forms breaks some built in
// request parsers like multer in express.
const options = new RequestOptions();
const formData = new FormData();
// Append files to the virtual form.
for (const file of files) {
formData.append(file.name, file)
}
// Send it.
return this.http.post(uploadUrl, formData, options);
}
Also don't forget to set the header 'Content-Type': undefined, I've scratched my head over this for hours.

Related

What is this kind of jpeg data and how can I use it as an image tag src to display it in a webpage?

I'm working in Angular trying to display some image data I'm getting from an external api. I've tried a few things like converting this data to a Blob to display an image but I'm not having any luck. This is what the data looks like:
����JFIF��C\t\t\t\n\n\n\n\n\n\t\n\n\n��+��\t\n���}!1AQa"q2���#B��R��$3br�\t\n%&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz���������������������������������������������������������������������������?��=��[��4�\r�j���U�t{��|���#&#w��Z�����R.��.��\r>�b��;2\��1#��>��KY�d�����#�;��k��Y\r���8e8S��u��JZ4�p���ֺ?�����19�dkr�j\r��ְ��"��.\to\b�ܗ��m��%�3�iI���j…��?�C�&k��d�=�����}҅#���5j�9m�+�����\Lm�9��~��lU#,W'i��Gn�D ��:b�����Lv�'�޵���.ck�c��<�w�$�!U��#��f��_%�8�f]j�(��2��\n�F�suu br?�Mew4��3��r=s�r��cs�#�ޑ�{ˋ���9<qP��|��I�OmZh7Z����泯�)��d�GR~��{}s<g�����+�Z)���'֦�Q�;�_���jr��ӧ!'��[S�/r�X���yI-�ЎO���ז��h�,����=��\n�?��b4{�aP�fx�����?ah�-�e�s�p7q����^c�,I#�����B��}��\\��_V�z���"i����ZcS�BT �s���}vK(ZY���>�^��o��zZ�m�a��qU�P��rO=�"�'Y�?���V�i��;��Sǵs����v����sI</�9'=sU�[X)݃��kv��\\d7j�ӡ�v�T���
Another piece of information that could be helpful is when I call the api from Postman, it displays the image correctly in the response body.
Any help is appreciated.
Thanks.
Edit:
Currently what I am doing is converting this jpeg data to a buffer, creating a blob from the buffer, using that blob to create an object url and assigning that object url to the image src. All this does is return the unable to load image icon.
const buffer = Buffer.from(image.data);
const blob = new Blob(buffer.data, { type: 'image/jpeg' });
const image = URL.createObjectURL(blob);
const imageTag = document.getElementById('fullImage');
(imageTag as HTMLImageElement).src = image;
Your blob doesn't seem to be valid base64.
In template you could do:
<img [src]="myImage">
Import DomSanitizer, define it in the constructor and then sanitize Base64 string you want to pass as your image source (use trustResourceUrl):
import { DomSanitizer } from '#angular/platform-browser';
constructor(private sanitizer: DomSanitizer) { }
base64string: string; // your blob here
myImage: string; // image string to template
blobToImage() {
this.myImage = this.sanitizer.bypassSecurityTrustResourceUrl('data:image/jpg;base64,'
+ base64string);
}
Angular http client converts requests to json unless you explicitly tell it not to. This could be why the image data you're getting isn't what you're expecting.
Try setting { responseType: 'blob' } in the get request options.
Thanks to the input from Mike and Joosep.P I learned that my image data was getting corrupted as it was passed back from my api. I decided to forgo calling the external api from my api and just did it from my front-end instead. Once I did that converting the blob data was easy.
this.imagingSerivce.getImage(imageId, response.server, response.token).subscribe((response2: any) => {
const imageTag = document.getElementById('fullImage');
const reader = new FileReader();
reader.readAsDataURL(response2);
reader.onloadend = () => {
(imageTag as HTMLImageElement).src = '' + reader.result;
};
});

Convert image/jpeg file sent as image/jpeg via REST API to AWS Lambda into a usable format

CURRENTLY
I am trying to send an image file to Lambda via API, such that it can be onsent to Textract.
Textract requires the file to be converted to "bytes", which I successfully achieved with this code: https://stackoverflow.com/a/69868101/1730260 but this code sent the file as a string, which only works for small files. Larger files converted to strings get too big i.e. bigger than 10MB, and then API Gateway will not accept.
So, instead, I am trying to send the image file in blob format.
I have a REST API request from Google Apps Script that looks like this:
Google Apps Script js:
let folder = DriveApp.getFolderById(folder_id);
let files = folder.getFiles();
let file = files.next();
let package = file.getBlob();
var options = {
method: "POST",
headers: {
"Content-Type": "image/jpeg",
},
payload: package,
};
var response = UrlFetchApp.fetch("https://api_address",options);
API Gateway Configurations
Type: LAMBDA_PROXY
Content Encoding: false
Binary Media Types: image/png image/jpeg
AWS Lambda js:
"use strict";
const AWS = require("aws-sdk");
exports.handler = async (event) => {
let file = event.body;
console.log("file:"+file)
let textract = new AWS.Textract();
let doc = params["doc"];
let config = {
Document: {
Bytes: Buffer.from(file), //<--- this is where issue is happening
FeatureTypes: ["TABLES"],
}
};
textract.analyzeDocument(config, function (err, data) {
console.log("analyzing...");
if (err) {
console.log(err, err.stack);
}
// an error occurred
else {
console.log("data:" + JSON.stringfy(data));
} // successful response
});
}
Console Log Output:
file:/9j/4AAQSkZJRgABAQAASABIAAD/4RvaRXhpZgAATU0AKgAAAAgABgESAAMAAAABAAYAAAEaAAUAAAABAAAAVgEbAAUAAAABAAAAXgEoAAMAAAABAAIAAAITAAMAAAABAAEAAIdpAAQAAAABAAAAZgAAAMAAAABIAAAAAQAAAEgAAAABAAeQAAAHAAAABDAyMjGRAQAHAAAABAECAwCgAAAHAAAABDAxMDCgAQADAAAAAf//AACgAgAEAAAAAQAAD8CgAwAEAAAAAQAAC9CkBgADAAAAAQAAAAAAAAAAAAYBAwADAAAAAQAGAAABGgAFAAAAAQAAAQ4BGwAFAAAAAQAAARYBKAADAAAAAQACAAACAQAEAAAAAQAAAR4CAgAEAAAAAQAAGrIAAAAAAAAASAAAAAEAAABIAAAAAf/Y/9sAhAABAQEBAQECAQECAwICAgMEAwMDAwQFBAQEBAQFBgUFBQUFBQYGBgYGBgYGBwcHBwcHCAgICAgJCQkJCQkJCQkJAQEBAQICAgQCAgQJBgUGCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQn/3QAEAAr/wAARCAB4AKADASIAAhEBAxEB/8QBogAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoLEAACAQMDAgQDBQUEBAAAAX0BAgMABBEFEiExQQYTUWEHInEUMoGRoQgjQrHBFVLR8CQzYnKCCQoWFxgZGiUmJygpKjQ1Njc4OTpDREVGR0hJSlNUVVZXWFlaY2RlZmdoaWpzdHV2d3h5eoOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4eLj5OXm5+jp6vHy8/T19vf4+foBAAMBAQEBAQEBAQEAAAAAAAABAgMEBQYHCAkKCxEAAgECBAQDBAcFBAQAAQJ3AAECAx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Q/SYuxn3C45WsxmGMmtKYAjn8qzZBg5xWiKkjntRtTK2fyqtEZIVwDjHpWpNhs9vQVQeJgMngU5EtWKo1Sa0vMRrtXaNxHfk12FhqqXYxnLY71wOoTxI6Rjjisjwm1/deMNSgnlxDELbyFPHLI5YL6/dzgVKRDdj2JsH5m4zx+VfMnx5hjl8QeGN5Chb2PsT/BMMDHTPv/hX09NGu0etfPXxb0q31DWNLkuGcGyYToFIALjco3ccjDHjjtV09xVNjtvtC6g91BOkeLS4MK7c52+WjDdn+L5u3GMVJ9liOhRhsEe31quw0u3lu2sJTI8l0WnGfuyeWg2jgcBQuKjkldNFQxHI6fTDUWHY6rwFYWcOqR3kMStILpACeowEr6o+JCwnwvqckh8sLaTneASVwh5wOTj2r478C+Jbez1+CzuuN93Gox6tsAr7N8ftDF4U1WW8G6NLSbcAM8bDnjI7e4rnqrVETPD/AAtoqRnVWniTabtdm0g5At4QTxnHOeDg+1bc2lW4BMOUPpU/hiOOP+1ect9uOeMDiGEDAyeMYx0+lXJXKls96fPqaUldHKy6esYO7is24tXH3elbtxnnzQNtU5p8Q7UGM1odFjlTazxzBl5Gcmvze+MR15fh/ef8JNa/ZbmK9vkVFj8pTEJ3MTKuBwyEHPfrX6feeip5khG0V8I/taPE+izMnQp0FaQfQyrxvE//1Ps2RpoOM59qrx3Fz6cCujuQjSciue8QXb6RpNxqNrD9okgQssQ4LkdAO....
UnsupportedDocumentException: Request has unsupported document format
NOTES
if I stringify the payload in the request, the size gets above 10MB and returns error HTTP content length exceeded 10485760 bytes
REQUEST
This is the format I need to get my code working (as it worked before with a different smaller image):
-1,-40,-1,-32,0,16,74,70,73,70,0,1,1,1,0,96,0,96,0,0,-1,-31,0,34,69,120,105,102,0,0,77,77,0,42,0,0,0,8,0,1,1,18,0,3,0,0,0,1,0,1,0,0,0,0,0,0,-1,-37,0,67,0,2,1,1,2,1,1,2,2,2,2,2,2,2,2,3,5,3,3,3,3,3,6,4,4,3,5,7,6,7,7,7,6,7,7,8,9,11,9,8,8,10,8,7,7,10,13,10,10,11,12,12,12,12,7,9,14,15,13,12,14,11,12,12,12,-1,-37,0,67,1,2,2,2,3,3,3,6,3,3,6,12,8,7,8,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,-1,-64,0,17,8,0,-96,1,86,3,1,34,0,2,17,1,3,17,1,-1,-60,0,31,0,0,1,5,1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,2,3,4,5,6,7,8,9,10,11,-1,-60,0,-75,16,0,2,1,3,3,2,4,3,5,5,4,4,0,0,1,125,1,2,3,0,4,17,5,18,33,49,65,6,19,81,97,7,34,113,20,50,-127,-111,-95,8,35,66,-79,-63,21,82,-47,-16,36,51,98,114,-126,9,10,22,23,24,25,26,37,38,39,40,41,42,52,53,54,55,56,57,58,67,68,69,70,71,72,73,74,83,84,85,86,87,88,89,90,99,100,101,102,103,104,105,106,115,116,117,118,119,120,121,122,-125,-124,-123,-122,-121,-120,-119,-118,-110,-109,-108,-107,-106,-105,-104,-103,-102,-94,-93,-92,-91,-90,-89,-88,-87,-86,-78,-77,-76,-75,-74,-73,-72,-71,-70,-62,-61,-60,-59,-58,-57,-56,-55,-54,-46,-45,-44,-43,-42,-41,-40,-39,-38,-31,-30,-29,-28,-27,-26,-25,-24,-23,-22,-15,-14,-13,-12,-11,-10,-9,-8,-7,-6,-1,-60,0,31,1,0,3,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,1,2,3,4,5,6,7,8,9,10,11,-1,-60,0,-75,17,0,2,1,2,4,4,3,4,7,5,4,4,0,1,2,119,0,1,2,3,17,4,5,33,49,6,18,65,81,7,97,113,19,34,50,-127,8,20,66,-111,-95,-79,-63,9,35,51,82,-16,21,98,114,-47,10,22,36,52,-31,37,-15,23,24,25,26,38,39,40,41,42,53,54,55,56,57,58,67,68,69,70,71,72,73,74,83,84,85,86,87,88,89,90,99,100,101,102,103,104,105,106,115,116,117,118,119,120,121,122,-126,-125,-124,-123,-122,-121,-120,-119,-118,-110,-109,-108,-107,-106,-105,-104,-103,-102,-94,-93,-92,-91,-90,-89,-88,-87,-86,-78,-77,-76,-75,-74,-73,-72,-71,-70,-62,-61,-60,-59,-58,-57,-56,-55,-54,-46,-45,-44,-43,-42,-41,-40,-39,-38,-30,-29,-28,-27,-26,-25,-24,-23,-22,-14,-13,-12,-11,-10,-9,-8,-7,-6,-1,-38,0,12,3,1,0,2,17,3,17,0,63,0,-3,-4,-94,-118,40,0,-94,-118,51,64,5,20,102,-118,0,40,-93,52,102,-128,10,40,-93,56,-96,2,-118,51,70,104,0,-94,-116,-47,64,5,20,81,64,5,20,102,-118,0,40,-93,52,80,1,69,20,80,1,69,25,-93,52,0,81,70,115,70,104,0,-94,-118,40,0,-94,-116,-47,-102,0,40,-94,-118,0,40,-93,52,102,-128,10,40,-51,20,0,81,69,25,-51,0,20,81,69,0,20,81,69,0,20,81,69,0,20,81,65,25,20,1,-7,47,-1,0,7,25,-1,0,-63,121,-11,111,-8,39,-57,-123,116,95,3,-4,18,-15,39,-128,117,47,-120,-98,32,-110,-10,-49,93,-100,106,16,106,58,-105,-126,-68,-91,-73,104,-53,-39,-85,31,46,105,68,-20,80,-36,46,-36,68,112,-115,-37,-33,-2,15,-2,-35,30,27,-8,-7,-1,0,4,-127,-43,-75,15,11,-4,92,-16,-25,-117,62,38,120,119,-32,-16,-44,-11,-7,52,95,18,-63,121,-84,-23,26,-127,-46,89,-102,107,-127,4,-122,88,38,-13,-43,-50,91,107,7,83,-36,87,-28,23,-4,29,-25,-1,0,4,-11,-16,111,-20,-51,-5,68,120,87,-30,-42,-123,-87,120,-102,-21,-60,-97,27,-81,-11,75,-67,118,-38,-2,-26,9,44,109,26,-46,59,4,-116,90,-94,66,-114,-128,-119,91,119,-104,-14,100,-127,-116,119,-5,-53,-2,9,-13,-1,0,4,-110,-8,111,-5,4,-1,0,-63,37,-66,38,124,81,-16,126,-75,-29,125,75,-60,31,25,-66,10,-117,-19,110,-33,89,-68,-74,-102,-50,-43,-37,74,-106,-28,-117,101,-118,-34,39,85,-33,51,-113,-99,-36,-32,47,57,-55,60,77,-33,47,-83,82,91,-21,126,-23,-88,-67,23,-111,-76,-76,-59,80,75,-82,-53,-70,-26,87,111,-51,108,121,7,-4,26,45,-5,108,-8,-5,-29,38,-105,-5,69,106,-1,0,25,62,46,120,-61,-59,90,71,-123,32,-48,110,34,-68,-15,-97,-118,110,47,-83,-76,120,-33,-5,75,-51,117,-110,-22,86,88,85,-74,38,-30,8,7,98,-25,-96,-81,-39,47,-126,63,-75,47,-61,63,-38,94,-38,-10,-29,-31,-57,-60,111,2,-4,65,-73,-45,89,82,-18,79,13,-21,-10,-102,-78,90,-79,-28,9,12,18,56,66,123,3,-118,-2,90,-1,0,-32,-34,95,-8,37,-60,-65,-16,85,-1,0,27,124,74,-16,63,-119,60,127,-30,-97,7,-4,39,-47,98,-45,-75,79,18,105,-38,4,-47,-59,117,-81,93,-122,-71,91,21,-52,-120,-15,-127,22,110,95,115,-58,-8,36,0,-71,109,-53,-42,-4,22,-8,53,117,-1,0,4,93,-1,0,-125,-103,60,39,-16,-49,-64,-34,46,-42,47,52,24,124,87,-91,120,125,-18,46,72,73,-75,29,55,86,-118,-40,-101,107,-91,-116,4,-112,-81,-38,83,-99,-95,119,-60,-110,5,66,6,-33,102,-93,-115,74,-16,-92,-43,-100,-94,-83,-14,73,107,-22,-1,0,-49,-56,-29,-42,49,-85,82,58,-14,-69,-65,77,54,-4,62,-13,-12,-61,-2,14,14,-8,89,-15,-125,-57,-97,-76,-33,-127,-18,62,29,-2,-38,31,13,-1,0,102,-99,46,15,15,121,119,90,23,-120,-2,46,94,-8,54,-29,86,-105,-19,50,-97,-76,-91,-68,10,86,101,-38,66,121,-121,-100,-95,94,-43,-6,-65,117,-81,89,-8,79,-62,-115,-88,106,-102,-123,-99,-91,-115,-115,-65,-99,115,123,115,58,-57,12,72,-85,-106,-111,-92,98,0,80,50,75,19,-46,-65,-100,79,-8,61,27,63,-16,-33,31,9,73,-1,0,-95,47,-113,-4,14,-72,-81,90,-1,0,-125,-51,60,115,-15,19,72,-16,-41,-64,-51,5,103,-43,97,-8,67,-86,-59,115,62,-93,21,-95,49,-61,123,-87,-60,-47,20,73,-37,5,75,44,37,-102,37,110,50,101,108,29,-68,112,81,111,-22,112,107,-19,84,-102,-41,-90,-69,-73,-67,-69,47,67,-94,-92,111,93,47,-18,39,-89,-53,75,126,-89,-20,111,-122,63,-32,-92,95,-77,-65,-115,60,81,30,-121,-93,124,122,-8,47,-85,-21,83,73,-28,-57,97,101,-29,109,50,123,-87,31,56,-38,34,73,-117,19,-98,49,-116,-26,-67,-81,-17,-118,-2,109,-4,47,-5,59,-1,0,-63,37,-1,0,110,-81,-128,-70,95,-123,-68,15,-29,-83,87,-10,117,-8,-101,42,64,127,-74,60,93,119,-88,25,81,-112,15,53,110,77,-52,-25,76,111,49,67,-1,0,-86,-106,44,57,82,-72,31,35,126,-23,127,-63,58,127,101,-85,31,-40,-33,-10,71,-16,-97,-128,116,111,-120,-98,38,-8,-95,-96,-23,118,-34,102,-107,-83,-21,87,80,93,72,-10,-78,124,-15,-57,12,-112,-94,-125,108,-96,-2,-20,51,57,85,96,-95,-54,4,11,-73,37,-94,-17,-70,-47,118,126,-116,-61,-38,93,-85,117,-35,117,94,-89,-69,63,8,107,-14,7,-32,31,-62,-49,-116,86,31,-16,95,75,-3,123,80,-3,-75,62,27,-8,-117,-31,-21,120,-105,90,-111,62,15,-37,-4,93,-66,-68,-42,-96,-123,-84,-18,-124,86,-89,67,101,-14,85,-32,98,-110,52,121,-60,98,34,-61,-107,21,-6,-5,39,-6,-93,-12,-81,-26,-109,-10,55,-1,0,-107,-56,-75,-81,79,-8,78,-4,83,-49,-3,-62,-11,10,-116,42,-26,-58,-58,31,-35,-109,-5,-83,-45,-11,53,-83,-90,26,114,-13,95,-87,-3,24,124,88,-8,-35,-32,-33,-128,126,23,58,-25,-114,-68,91,-31,-113,6,104,-95,-4,-77,-88,107,-70,-84,26,117,-82,-20,19,-73,-51,-103,-107,115,-128,78,51,-40,-41,55,-16,115,-10,-42,-8,57,-5,67,-21,114,105,-65,15,-66,45,124,52,-15,-34,-95,18,-105,123,95,15,120,-98,-53,84,-103,64,25,36,-92,18,-69,0,7,-75,127,55,-65,-16,88,-17,-118,-102,76,-33,-16,113,6,-83,15,-19,117,97,-29,111,16,124,24,-16,-19,-36,80,-40,-24,-6,68,-51,9,26,59,-38,-122,-73,107,113,-67,9,-119,-25,59,-90,49,58,-69,17,48,86,-36,-95,107,-24,-49,-121,127,-16,76,-1,0,-8,38,-17,-19,-9,-15,-37,-64,122,-73,-20,-43,-5,67,95,124,19,-42,-76,-55,-48,73,-31,-21,93,78,-14,-41,90,-44,-18,-53,6,-127,-84,95,86,-109,-49,75,-108,96,-64,-104,60,-27,63,41,10,-72,-36,-58,30,-11,35,25,-67,-91,-37,116,-68,-41,127,33,86,-3,-37,113,-66,-85,-85,-39,-7,35,-6,6,92,-48,79,56,-23,89,-2,28,-45,38,-48,-4,59,99,99,54,-95,121,-85,77,103,111,28,18,95,93,-120,-59,-59,-37,42,-123,50,-55,-27,-94,71,-67,-120,-36,118,34,-82,73,-62,-127,-127,89,127,22,116,29,115,-59,-97,11,-68,67,-91,-8,103,92,-113,-61,30,35,-44,116,-21,-117,109,51,87,-110,-48,94,46,-105,114,-15,-78,-59,112,97,44,-94,79,45,-56,109,-123,-128,59,113,-102,-102,-115,-59,54,-75,-73,-30,58,122,-38,-3,78,71,-29,47,-19,-73,-16,103,-10,116,-41,-93,-46,126,33,124,91,-8,103,-32,93,86,104,-60,-47,-39,-8,-121,-59,22,58,93,-60,-120,122,48,-114,121,81,-120,-29,-82,49,93,87,-62,-17,-116,-34,17,-8,-29,-31,72,-11,-17,4,-8,-85,-61,-66,49,-48,-26,109,-111,-22,58,30,-91,14,-95,105,35,0,9,2,88,89,-112,-112,8,-17,-36,87,-32,42,127,-63,-75,-33,2,126,2,-8,-69,-57,26,-41,-19,-91,-5,93,-23,54,122,-10,-85,124,111,-76,-87,52,-49,19,89,-23,122,-90,-92,-81,-103,38,-70,-68,93,70,41,-26,-106,121,29,-119,-39,18,-74,54,-106,50,57,124,39,-127,-1,0,-63,-74,127,19,-96,-3,-97,127,-32,-68,-119,-32,63,-121,-98,49,-44,60,65,-16,-33,-59,-105,58,-26,-122,-105,107,27,67,23,-120,-84,109,-83,-82,-82,44,-18,100,-123,-62,-107,111,-36,35,-126,84,50,-17,97,-128,25,-123,107,-121,-118,-87,63,103,125,108,-35,-42,-38,43,-40,49,30,-28,28,-29,-78,105,107,-66,-67,79,-22,112,114,42,-98,-67,-81,-39,120,91,70,-70,-44,-75,75,-53,93,55,79,-79,-119,-89,-71,-70,-70,-103,97,-122,-34,53,25,103,119,98,21,84,14,73,39,2,-82,-109,-111,95,-126,-97,-16,122,-81,-114,-2,35,104,-10,-33,6,116,17,54,-83,15,-62,29,85,-81,39,-44,34,-76,38,56,47,117,56,-98,50,-119,59,96,-87,101,-124,-77,68,-83,-58,76,-83,-125,-73,-114,122,-110,105,-58,11,121,59,93,-20,-68,-39,116,-29,-51,123,-10,-71,-5,3,-31,-97,-8,41,23,-20,-17,-29,79,20,71,-94,104,-65,30,-66,11,-22,-38,-44,-46,121,49,-40,89,120,-33,76,-72,-70,119,-50,54,-120,-110,98,-60,-25,-116,99,57,-81,89,-15,71,-118,116,-65,2,-8,103,80,-42,-75,-115,66,-57,72,-47,-76,-101,105,47,111,-81,-81,110,18,-34,-38,-54,8,-44,-68,-110,-53,35,-112,-88,-118,-96,-77,51,16,0,4,-109,95,-50,79,-122,63,103,127,-8,36,-73,-19,-43,-16,19,74,-16,-65,-127,-4,117,-86,-2,-50,-65,19,38,72,15,-10,-49,-117,-81,53,3,42,50,1,-26,-83,-55,-71,-100,-23,109,-26,0,-1,0,-22,-91,-117,14,84,-82,7,-56,-33,-85,-66,49,-3,-105,-84,127,99,-113,-8,32,-17,-59,79,1,104,-1,0,17,60,75,-15,59,65,-45,62,25,107,-14,105,90,-34,-75,117,5,-52,-113,107,38,-99,59,-57,28,50,66,-118,13,-80,7,-9,97,-103,-54,-85,5,14,80,32,91,-81,-5,-70,51,-101,-34,59,118,122,116,100,97,-1,0,121,86,20,-42,-46,122,-9,71,-45,62,12,-3,-74,126,13,124,71,-16,22,-79,-30,-81,14,-4,91,-8,103,-81,-8,95,-61,-82,-79,-22,-70,-66,-101,-30,-117,27,-85,13,49,-40,101,86,121,-29,-108,-57,17,35,-96,118,25,-83,-97,-126,-1,0,-76,-89,-61,-81,-38,75,73,-70,-44,62,30,120,-13,-63,-98,61,-80,-79,113,21,-59,-49,-121,53,-69,109,86,24,28,-25,10,-17,3,-72,83,-63,-32,-98,-43,-4,-73,127,-63,-67,127,-16,72,104,-1,0,-32,-81,30,34,-15,-18,-125,-29,111,28,120,-53,-61,127,9,-4,10,109,117,11,-51,55,67,-70,88,-28,-44,53,43,-107,-98,56,29,4,-55,36,8,-55,28,50,-18,-112,-60,-49,-126,-86,48,27,35,-109,-15,55,-20,-83,-15,27,-2,9,-7,-1,0,5,-84,-15,39,-20,-33,-16,63,-30,87,-119,60,51,117,-30,-83,94,-33,-64,-42,-6,-22,-56,18,-22,77,47,85,-114,6,-1,0,72,-14,-128,5,-93,-114,117,37,-93,8,-37,-94,-34,-98,91,109,-58,-114,-101,85,85,30,-78,87,75,-18,-33,-17,-5,-75,38,82,92,-110,-102,122,65,-21,-23,-1,0,13,-8,-24,127,84,-6,119,-19,-89,-16,119,87,-8,-78,-34,0,-76,-8,-79,-16,-42,-21,-57,107,49,-73,111,13,-59,-30,123,39,-43,-42,65,-43,13,-88,-105,-50,-36,61,54,-26,-67,57,8,-57,-14,-81,-27,-85,-2,14,0,-1,0,-126,6,120,51,-2,9,23,-16,87,-31,-97,-114,-68,1,-29,111,25,107,-17,-81,106,-115,-93,-21,63,-37,-113,110,-52,47,4,6,104,-25,-74,48,-57,25,-119,15,-107,54,81,-116,-124,124,-97,63,28,-2,-42,126,-53,67,-30,-97,-4,20,35,-2,8,65,-32,56,116,95,-119,87,94,7,-8,-103,-29,-65,4,91,89,-65,-116,94,-35,-81,46,-94,-111,72,-118,105,-2,89,35,113,52,-79,-58,-29,-51,13,-71,26,77,-29,44,5,102,-41,-18,37,86,58,-72,-76,-97,77,89,95,-14,-10,49,122,41,43,-90,-9,-75,-19,-87,-12,87,-60,-65,-8,40,47,-64,95,-126,-98,48,-72,-16,-1,0,-116,-66,55,124,35,-16,-106,-65,104,64,-97,77,-42,-68,97,-89,88,94,67,-98,70,-24,-91,-103,92,117,-18,43,-46,60,29,-29,-67,23,-30,71,-123,-19,117,-65,14,-22,-6,94,-67,-92,95,-89,-103,107,127,-89,93,71,117,109,114,-67,50,-110,33,42,-61,32,-14,15,106,-2,117,127,-30,29,63,-39,67,-10,76,-8,59,-30,24,-1,0,106,47,-38,-41,67,-48,126,41,90,-51,119,37,-75,-121,-122,-4,65,98,-111,91,68,1,16,121,-106,51,-37,-55,125,115,33,32,59,-120,-62,99,126,-59,-55,30,99,65,-1,0,6,93,124,109,-41,116,-97,-37,39,-30,-121,-61,120,-75,-117,-55,-68,39,-86,120,81,-11,-1,0,-80,18,126,-51,-10,-69,123,-69,88,22,-31,84,-97,-111,-52,119,12,-89,3,-26,1,119,125,-59,-59,81,-116,106,115,65,61,82,-67,-42,-37,106,-67,73,-84,-35,52,-90,-74,-70,86,-21,-85,74,-25,111,-5,41,126,-38,63,24,-68,71,-1,0,7,92,-21,31,14,-11,15,-117,31,18,-81,-66,31,71,-29,63,18,90,-89,-123,-18,60,79,123,46,-116,-80,-59,-89,95,60,81,-117,54,-108...
How do I convert the string currently in my console log to the above format?
In case you are struggling with the API-gateway limit, then a better way would be to use Pre-signed URLs
You can upload upto 5GB with this method.
You can create a dedicated bucket for this purpose and configure a trigger.
As soon as a file is uploaded to bucket, lambda will be triggered and it can process your image.
You can upload the image in any appropriate format that you want.

How to use a Javascript file to refresh/reload a div from an HTML file?

I am using Node JS and have a JS file, which opens a connection to an API, works with the receving API data and then saves the changed data into a JSON file. Next I have an HTML file, which takes the data from the JSON file and puts it into a table. At the end I open the HTML file in my browser to look at the visualized table and its data.
What I would like to happen is, that the table (or more specific a DIV with an ID inside the table) from the HTML file refreshes itself, when the JSON data gets updated from the JS file. Kinda like a "live table/website", that I can watch change over time without the need to presh F5.
Instead of just opening the HTML locally, I have tried it by using the JS file and creating a connection with the file like this:
const http = require('http');
const path = require('path');
const browser = http.createServer(function (request, response) {
var filePath = '.' + request.url;
if (filePath == './') {
filePath = './Table.html';
}
var extname = String(path.extname(filePath)).toLowerCase();
var mimeTypes = {
'.html': 'text/html',
'.css': 'text/css',
'.png': 'image/png',
'.js': 'text/javascript',
'.json': 'application/json'
};
var contentType = mimeTypes[extname] || 'application/octet-stream';
fs.readFile(filePath, function(error, content) {
response.writeHead(200, { 'Content-Type': contentType });
response.end(content, 'utf-8');
});
}).listen(3000);
This creates a working connection and I am able to see it in the browser, but sadly it doesn't update itself like I wish. I thought about some kind of function, which gets called right after the JSON file got saved and tells the div to reload itself.
I also read about something like window.onload, location.load() or getElementById(), but I am not able to figure out the right way.
What can I do?
Thank you.
Websockets!
Though they might sound scary, it's very easy to get started with websockets in NodeJS, especially if you use Socket.io.
You will need two dependencies in your node application:
"socket.io": "^4.1.3",
"socketio-wildcard": "^2.0.0"
your HTML File:
<script type="module" src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.0.0/socket.io.js"></script>
Your CLIENT SIDE JavaScript file:
var socket = io();
socket.on("update", function (data) { //update can be any sort of string, treat it like an event name
console.log(data);
// the rest of the code to update the html
})
your NODE JS file:
import { Server } from "socket.io";
// other code...
let io = new Server(server);
let activeConnections = {};
io.sockets.on("connection", function (socket) {
// 'connection' is a "magic" key
// track the active connections
activeConnections[socket.id] = socket;
socket.on("disconnect", function () {
/* Not required, but you can add special handling here to prevent errors */
delete activeConnections[socket.id];
})
socket.on("update", (data) => {
// Update is any sort of key
console.log(data)
})
})
// Example with Express
app.get('/some/api/call', function (req, res) {
var data = // your API Processing here
Object.keys(activeConnections).forEach((conn) => {
conn.emit('update', data)
}
res.send(data);
})
Finally, shameful self promotion, here's one of my "dead" side projects using websockets, because I'm sure I forgot some small detail, and this might help. https://github.com/Nhawdge/robert-quest

How to save imported JSON file with Expo Filesystem

I have been working on a React Native project with Expo that uses a json file to store local data. I am importing the data like so
import data from '../database.json'
I am making changes (adding and removing) to the imported JSON by using data.push(new_data). These changes are not persistent when I close the app because I cannot figure out how to save them. I have looked at using the expo-file-system library as so:
import * as FileSystem from 'expo-file-system';
...
FileSystem.writeAsStringAsync(FileSystem.documentDirectory + 'database.json', data);
This is from looking at examples in the API documentations. This however always throws promise rejections and doesn't end up writing the file. Can you point me in the right direction?
Also, should I import the database.json in a different way so I will already have the uri to save it to?
The documentation doesn't give an example of it's returned props in promises, so I was overlooking it for longer than I care to admit 😅. I was really dedicated to figuring this out so I could use the Expo solution, and totally missed the return Promise for createFileAsync, so hopefully this saves someone a significant amount of time in the future.
import * as FileSystem from 'expo-file-system';
const { StorageAccessFramework } = FileSystem;
const saveFile = async () => {
const permissions = await StorageAccessFramework.requestDirectoryPermissionsAsync();
// Check if permission granted
if (permissions.granted) {
// Get the directory uri that was approved
let directoryUri = permissions.directoryUri;
let data = "Hello World";
// Create file and pass it's SAF URI
await StorageAccessFramework.createFileAsync(directoryUri, "filename", "application/json").then(async(fileUri) => {
// Save data to newly created file
await FileSystem.writeAsStringAsync(fileUri, data, { encoding: FileSystem.EncodingType.UTF8 });
})
.catch((e) => {
console.log(e);
});
} else {
alert("You must allow permission to save.")
}
}
Use AsyncStorage instead. The react native package is deprecated but working, or use #react-native-community/async-storage and convert json to string (AsyncStorage can only store strings)
Set item
import AsyncStorage from '#react-native-community/async-storage';
...
await AsyncStorage.setItem('myData', JSON.stringify(data))
Get item
const data = await AsyncStorage.getItem('myData')
I found #JayMax answer very helpful however it's only for Android.
On iOS all you need to do is use Sharing.shareAsync and then you can save data to the file. Check this example:
const fileUri = FileSystem.documentDirectory + 'data.txt';
FileSystem.writeAsStringAsync(fileUri, 'here goes your data from JSON. You can stringify it :)', {
encoding: FileSystem.EncodingType.UTF8,
});
const UTI = 'public.text';
Sharing.shareAsync(fileUri, {UTI}).catch((error) => {
console.log(error);
});
If you using AsyncStorage, it only store for small data. Maybe 6mb or 10 mb.
You can use expo fileSystem
import * as FileSystem from 'expo-file-system';
...
FileSystem.writeAsStringAsync(FileSystem.documentDirectory + 'database.json', data);
Convert your data (Type json to string) Such as this:
writeData = async () => {
var persons = ''
await axios.get(`http://192.168.0.48:4000/api/sql/student`)
.then(res => {
persons = res.data
})
await FileSystem.writeAsStringAsync(FileSystem.documentDirectory + `offline_queue_stored.json`, JSON.stringify(persons));
}
#1.If the JSON File is in your Project Folder (PC/Laptop)
import data from './database.json';
#2. If the JSON File is in your Phone
import * as FileSystem from 'expo-file-system';
import * as DocumentPicker from 'expo-document-picker';
this.state = {
fileURI: null,
};
componentDidMount = () =>{
this._pickDocument();
}
_pickDocument = async () => {
let result = await DocumentPicker.getDocumentAsync({});
this.setState({
fileURI: result.uri
})
let fileData = await FileSystem.readAsStringAsync(this.state.fileURI)
console.log(fileData)
};

ProgressEvent.load is always the same as ProgressEvent.Total which causes the progress to fake

I'm trying to implement progress bar on a website.
The Problem:
ProgressEvent.load is always the same as ProgressEvent.Total which prevent the progress to show the real state of the upload. At the first second the xhr request does sent it looks like it finished but actually the server is still getting parts of the file.
JS:
My js code(the part of the progress) looks like that:
xhr.upload.onprogress = function (event) {
var progress = Math.round(event.lengthComputable ? event.loaded * 100 / event.total : 0);
that._onProgressItem(item, progress);
};
the property lengthComputable is true.
the event.loaded is 4354707 as the event.total which is 4354707.
C# Server Side:
public async Task<FileResultViewModel> Upload(string type)
{
string ServerUploadFoler = "...";
// Verify that this is an HTML Form file upload request
if (!Request.Content.IsMimeMultipartContent())
{
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.UnsupportedMediaType));
}
// Create a stream provider for setting up output streams
var streamProvider = new MultipartFormDataStreamProvider(ServerUploadFolder);
// Read the MIME multipart asynchronously content using the stream provider we just created.
await Request.Content.ReadAsMultipartAsync(streamProvider);
string guid = String.Empty;
if (serverUploadMoveFolder != ServerUploadFolder)
{
foreach (MultipartFileData fileData in streamProvider.FileData)
{
guid = Guid.NewGuid().ToString();
string newFileName = serverUploadMoveFolder + guid + GetExtension(uploadType);
FileInfo fi = new FileInfo(fileData.LocalFileName);
fi.MoveTo(newFileName);
}
}
// Create response
return new FileResultViewModel
{
FileName = guid
};
}
Chrome debug after 1 second of upload with a file of 4.2MB:
In fiddler after the request has completed:
My questions are:
How does the browser knows the loaded size? How does it split the file to parts and based on what params?
How do the xhr.upload.onprogress function event get updated with the progress? Does it the server which report about his progress and if it is so where is it on the code because I didn't handle it.
Why doesn't the loaded property show the real size of part?