如何使用NativeScript照相机拍摄照片并将其上载到服务器

如何使用NativeScript照相机拍摄照片并将其上载到服务器,nativescript,nativescript-vue,Nativescript,Nativescript Vue,我是Nativescript Vue开发的新手,我正在尝试拍摄照片并将其发送到服务器。我的代码在Android上运行良好,但当我在iOS上运行时,会出现错误,图像甚至不会粘贴到页面上,也不会上传到服务器上 import * as camera from "nativescript-camera"; import * as bghttp from "nativescript-background-http"; const firebase = require("nativescript-plugi

我是Nativescript Vue开发的新手,我正在尝试拍摄照片并将其发送到服务器。我的代码在Android上运行良好,但当我在iOS上运行时,会出现错误,图像甚至不会粘贴到页面上,也不会上传到服务器上

import * as camera from "nativescript-camera";
import * as bghttp from "nativescript-background-http";
const firebase = require("nativescript-plugin-firebase");
var session = bghttp.session("image-upload");

takePicture() {
                camera.requestPermissions()
                    .then(() => {
                        camera.takePicture({ width: 300, height: 300, keepAspectRatio: true, saveToGallery:true })
                            .then(imageAsset => {
                                this.img = imageAsset.android;
                            })
                            .catch(e => {
                                console.log('error:', e);
                            });
                    })
                    .catch(e => {
                        console.log('Error requesting permission');
                    });
            } 
upload() {

                var file =  this.img;
                var url = "https://bocorp.ru/assets/mobileNewOrder.php";
                var name = file.substr(file.lastIndexOf("/") + 1);

                // upload configuration
                var bghttp = require("nativescript-background-http");
                var session = bghttp.session("image-upload");
                var request = {
                    url: url,
                    method: "POST",
                    headers: {
                        "Content-Type": "application/octet-stream",
                        "File-Name": name,
                    },
                    content: JSON.stringify({
                        Title: title
                    }),
                    description: "Uploading " + name
                };
                var task = session.uploadFile(file, request);


我知道应该在this.img=imageAsset.android中使用另一个代码;但是我不明白我怎么能从Iphone相机里拍到照片。如果有任何提示

我们将图像保存到设备上,然后稍后以多部分上传的方式进行上传,我将非常高兴。您可能可以跳过文件保存部分,但它确实允许我们避免读取整个图像,以便稍后在我们的应用程序流中上载。我想,如果您已经有了用于显示的图像源,您可以将其重新用于在同一页面上上载

希望这对你有所帮助

const imageSource=需要“tns-core-modules/图像源” // ... 照相机。拍摄照片照相机镜头 .thenimageAsset=>{ 返回imageSource.fromAssetimageAsset } .thenimageSource=>{ 让pathDest='/path/on/device'//您定义 console.log`Created image source with width=${imageSource.width}height=${imageSource.height}位于${pathDest}` imageSource.saveToFilepathDest,“jpg”,50 return pathDest//保存此文件以便以后查找 } 然后当我们需要上传的时候

const mime=需要“mime-types” 将*作为bghttp从“nativescript后台http”导入 ... 让session=bghttp.session'image-upload' 让请求={ 网址:'https://yourendpoint.com/here', 方法:“POST”, androidAutoDeleteAfterUpload:true, 标题:{ “内容类型”:“应用程序/八位字节流”, } } //光路不知何故是已知的。我们使用Vuex,但不知怎的,它进入了这个页面 设params=[ {name:'photo1',filename:photoPath,mimeType:mime.lookupphotoPath} ] 返回新的PromiseSolve,拒绝=>{ 让task=session.multipatuploadparams,请求 task.on'error',e=>{ 拒绝 } task.on'complete',res=>{ 决定 } }
您必须使用ImageSource模块将照片保存到文件中,然后使用保存的图像上载到服务器。