Http post - Get and save the Base64 string(.zip file) response from API server


#1

Hey all!

Tried for - Get the base64 string(Encoded .zip file) response from my API server and decode the base64 string response to .zip file then store it in local Filesystem.

  • Nativescript with Angular

So far tried :

Http post call with datas -

    getZipfile() {
        let headers = new Headers();
        headers.append("Content-Type", "text/plain;charset=UTF-8");
        // headers.append("Content-Type", " application/json");
       // headers.append("Content-Type", " application/base64");
 this.http.post(this.baseapiUrl + "****/*****/******/" + 1001 + "/602147" + "/2" + "/1", { headers: headers })
     .map(e => e.text())
            .subscribe(e => {
                console.log(e);
            }, (e) => {
                console.log(e);
            });
    }

Response :
Its connected to server but thrown below error,

 ERROR: Response with status: 200 for URL: null

If tried Postman:

I am getting the base64 response in postman. Headers info,

cache-control →no-cache, no-store, max-age=0, must-revalidate
content-length →17440
content-type →text/plain;charset=UTF-8
date →Fri, 29 Dec 2017 05:38:21 GMT
expires →0
pragma →no-cache

Please suggest! How to achieve this!


#2

Hi bro,

I am using NS-Camera and NS Image picker …the selected image or taken image covert to base64 for this to loopback model (database)… but the converted base64 String code have MB’s of Size… it’s difficult store in db … Is There any base64 String code compressor …


#3

Hi @hariaravind10, you should not compress the base64 string, but the image itself. Here’s a snippet I wrote recently (the most interesting bit is at asset.options):

  private pickImage(): void {
    const imagePicker = createImagePicker({
      minimumNumberOfSelection: 1,
      maximumNumberOfSelection: 1,
      mediaType: ImagePickerMediaType.Image,
      prompt: "Select a logo for your printed receipt",
      mode: "single"
    });

    imagePicker
        .authorize()
        .then(() => imagePicker.present())
        .then((selection: Array<ImageAsset>) => {
          selection.forEach(asset => {
            asset.options = {
              // note that these numbers get doubled.. prolly based on screenres
              width: 150,
              height: 150,
              keepAspectRatio: true
            };
            new ImageSource().fromAsset(asset).then(src => {
              const base64Encoded = src.toBase64String("png");
              // store the encoded string in appsettings so we can easily retrieve it later
              this.applicationSettingsService.setPrintLogo(base64Encoded);
              // assign the image to a variable so we can show it in an 'Image' tag on screen
              this.logo = src;
            });
          });
        })
        .catch(e => {
          console.log("imagePicker selection error: " + e);
          if (("" + e).indexOf("Authorization failed") > -1) {
            this.toastService.show("Go to the settings and allow this app to access your photo library.", true);
          }
        });
  }


#4

Hi @Eddy , I tried with asset.options —>still i am getting thousands of base64 string …