bykof / cordova-plugin-webserver

A webserver plugin for cordova
Other
119 stars 51 forks source link

Feature request: From base64 to response #54

Closed lasterra closed 4 years ago

lasterra commented 4 years ago

Hello, in my project i have files stored inside an sqlite database. I want that files to be response by local webserver, so with the actual implemention i need to write them on disk to be response later using the "path" property.

For me it would be great it the webserve can decode a base64 string, it is a tricky implementation for android but i have no idea to do it for iOS.

In NanoHTTPDWebserver-->serve i add this condition

else if (responseObject.has("base64")){ try { String base64 = responseObject.getString("base64"); byte[] bytes = Base64.decode(base64, Base64.DEFAULT); return newFixedLengthResponse( Response.Status.lookup(responseObject.getInt("status")), getContentType(responseObject), new ByteArrayInputStream(bytes), bytes.length); } catch (JSONException e) { e.printStackTrace(); } return response; }

Do you think it has sense to add this feature in your project?

Regards, Enrique

bykof commented 4 years ago

No that would not make sense. There is a package in javascript https://www.npmjs.com/package/js-base64 which can do it.

lasterra commented 4 years ago

i know that i can decode the base64 string with JS, but how can i put it in the webserver response?

bykof commented 4 years ago

When you call sendResponse you can encode the body with js-base64

digaus commented 4 years ago

When you call sendResponse you can encode the body with js-base64

I am trying to send a base64 zip file as a body. However the resulting zip file is always damaged or faulty.

I have this data: data = 'data:application/zip;base64,xxxxxxx'

What I tried:

 headers:  {
       'Content-Type': 'application/zip'
 }

body: data body: data.spllit(',')[1] body: atob(data.split(',')[1]) body: atob(data)

Nothing worked :/ any idea?

danielehrhardt commented 3 years ago

When you call sendResponse you can encode the body with js-base64

I am trying to send a base64 zip file as a body. However the resulting zip file is always damaged or faulty.

I have this data: data = 'data:application/zip;base64,xxxxxxx'

What I tried:

 headers:  {
       'Content-Type': 'application/zip'
 }

body: data body: data.spllit(',')[1] body: atob(data.split(',')[1]) body: atob(data)

Nothing worked :/ any idea?

Are you able to make it work?