I am able to upload a file on android to a C# web api, using nativescript-camera with nativescript-background-http.
I set the params as follows:
var params = [{ name: "image", filename: fileUri, mimeType: 'application/octet-stream' }];
...
And send the request:
let request = {
url: this.API_URL + "/UploadOctetFile",
method: "POST",
headers: {
"Authorization": accessTokenJson,
"Content-Type": "application/octet-stream",
"File-Name": imageName
},
description: "{ 'uploading': " + imageName + " }"
};
return this.session.multipartUpload(params, request);
In android the fileUri is returned in a fairly straight forward manner:
takePictrue.then((imageAsset: ImageAsset) => {
Then imageAsset.android has the file uri that is needed to send the image. However, for ios, imageAsset.ios is a PHAsset. To get the file URI, I use the following code:
let manager = PHImageManager.defaultManager()
let options = new PHImageRequestOptions();
options.resizeMode = PHImageRequestOptionsResizeMode.Exact;
options.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
manager.requestImageForAssetTargetSizeContentModeOptionsResultHandler(imageAsset.ios, { width: 2048, height: 1536 }, PHImageContentModeAspectFill, options, function (result, info) { let srcfilePath = info.objectForKey("PHImageFileURLKey").toString(); ... });
In the above: srcfilePath (which I pass as fileUri) is: file:///var/mobile/Media/DCIM/101APPLE/IMG_1902.JPG
The image name is then IMG_1902.JPG
However, this uploads an empty file. I'm guessing that I need to change the fileUri or perhaps I should be using a different method to: requestImageForAssetTargetSizeContentModeOptionsResultHandler.
I have tried the solution suggested here to do:
let fileUri = image.fileUri.replace("file://","");
and I've tried changing the mime type "mimeType":"image/jpg".
Any ideas are much appreciated.