Android 如何以';文件';http post正文的内部
我需要将一个图像作为“文件”发送到我的服务器,但我不知道怎么做。我正在使用nativescript camera plus和nativescript后台http插件发送请求。这是代码Android 如何以';文件';http post正文的内部,android,typescript,nativescript,nativescript-angular,nativescript-background-http,Android,Typescript,Nativescript,Nativescript Angular,Nativescript Background Http,我需要将一个图像作为“文件”发送到我的服务器,但我不知道怎么做。我正在使用nativescript camera plus和nativescript后台http插件发送请求。这是代码 onTakePictureTap() { requestPermissions().then( () => { takePicture({ width: this.width, height: this.hei
onTakePictureTap() {
requestPermissions().then(
() => {
takePicture({ width: this.width, height: this.height, keepAspectRatio: this.keepAspectRatio, saveToGallery: this.saveToGallery, allowsEditing: this.allowsEditing })
.then((imageAsset: any) => {
this.cameraImage = imageAsset;
let that = this;
imageAsset.getImageAsync(function (nativeImage, ex) {
if (ex instanceof Error) {
throw ex;
} else if (typeof ex === "string") {
throw new Error(ex);
}
let imgPhoto = new ImageSource();
imgPhoto.fromAsset(imageAsset).then((imgSrc) => {
这是我设置nativescript后台http的地方
var bghttp = require("nativescript-background-http");
var session = bghttp.session("image-upload");
let token = JSON.parse(appSettings.getString('token'));
这是请求
var request = {
url: environment.apiUrl + 'users/1/photos',
method: "POST",
headers: {
"Content-Type": "form-data/File",
"Authorization": "Bearer " + token
},
description: "Uploading "
};
var task = session.uploadFile(imgSrc, request);
task()
},
err => {
console.log('Error getting image source: ');
console.error(err);
alert('Error getting image source from asset');
});
});
}, (error) => {
console.log("Error: " + error);
});
},
() => alert('permissions rejected')
);
}
这是任务,我发送图像和请求
var request = {
url: environment.apiUrl + 'users/1/photos',
method: "POST",
headers: {
"Content-Type": "form-data/File",
"Authorization": "Bearer " + token
},
description: "Uploading "
};
var task = session.uploadFile(imgSrc, request);
task()
},
err => {
console.log('Error getting image source: ');
console.error(err);
alert('Error getting image source from asset');
});
});
}, (error) => {
console.log("Error: " + error);
});
},
() => alert('permissions rejected')
);
}
我认为问题在于它是作为ImageAsset而不是“文件”发送的(但我不确定)。我不认为我需要改变文件类型,我只需要让它知道这是一个'文件'像我在邮递员
这就是《邮递员》中的样子(这部作品)
这里有一个最小的解决方案:
document.getElementById('file').addEventListener('change',function(){
取('https://api.imgur.com/3/upload“,{method:'POST',body:document.getElementById('file').files[0]});
});代码>
没错,至少在iOS上,映像资产不是一个文件。它只包含对PHAsset的引用
另外,您似乎正在将图像源直接传递给插件。它应该是文件路径。您必须将图像写入本地文件夹,然后使用文件路径。在ImageSource实例上使用saveToFile(…)
方法,然后将文件路径传递给后台http插件