Capacitor: How i can send files from camera with CameraResultType.Uri?

Created on 27 Sep 2018  路  3Comments  路  Source: ionic-team/capacitor

import { Plugins, CameraResultType } from '@capacitor/core';

const { Camera } = Plugins;

async takePicture() {
  const image = await Camera.getPhoto({
    quality: 90,
    allowEditing: true,
    resultType: CameraResultType.Uri
  });

  var imageUrl = image.webPath;
  imageElement.src = imageUrl;
}

How i can send form with a camera image file?

Most helpful comment

Capacitor doesn't provide a plugin or method for uploading files, you can use XHR or fetch to do so.

As example of how you can do it.

get the blob from the url:

let blob = await fetch(cameraResult.webPath).then(r => r.blob());

or get the blob from the base64:

let blob = b64toBlob(contents.data, "image/jpg", 512);

(google for b64toBlob method).

Once you have the blob, append it to FormData

let formData = new FormData();
formData.append("image", blob);

And upload it with XHR or fetch (example for XHR)

var oReq = new XMLHttpRequest();
oReq.open("POST", "https://yourserver.com/upload.php", true);
oReq.send(formData);

All 3 comments

Hey Daniel, did you solve this? Could the capacitor team document this better?

i have sent as base64, then i have converted to file on back end;

Capacitor doesn't provide a plugin or method for uploading files, you can use XHR or fetch to do so.

As example of how you can do it.

get the blob from the url:

let blob = await fetch(cameraResult.webPath).then(r => r.blob());

or get the blob from the base64:

let blob = b64toBlob(contents.data, "image/jpg", 512);

(google for b64toBlob method).

Once you have the blob, append it to FormData

let formData = new FormData();
formData.append("image", blob);

And upload it with XHR or fetch (example for XHR)

var oReq = new XMLHttpRequest();
oReq.open("POST", "https://yourserver.com/upload.php", true);
oReq.send(formData);
Was this page helpful?
0 / 5 - 0 ratings

Related issues

moberwasserlechner picture moberwasserlechner  路  3Comments

json-derulo picture json-derulo  路  3Comments

ebk46 picture ebk46  路  3Comments

peterpeterparker picture peterpeterparker  路  3Comments

natevw picture natevw  路  3Comments