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?
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);
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
And upload it with XHR or fetch (example for XHR)