【问题标题】:Apache Cordova error uploading cropped image to server from IOSApache Cordova 将裁剪的图像从 IOS 上传到服务器时出错
【发布时间】:2016-01-18 14:33:54
【问题描述】:

在我的 IOS 移动应用程序上,我正在拍照,然后从图库中选择一张照片。选择这张图片后,我正在使用 ng-img-crop.js 来裁剪这张图片并且不保存这张裁剪的图片,尝试将它上传到服务器,服务器端是 C# WCF。 (在这个特定的例子中,我试图上传本地 IIS)但是我收到了这个错误:

ERROR: {"code":1,"source":"data:image/png;base64,iVBORw0KGgoAAAANSUhE...","target":"http://11.111.11.111/wcf/OCRService.svc/upload","http_status":null,"body":null,"exception":null}
app.js (76,24)

在上传到服务器之前,我应该将此裁剪后的图像保存到图库吗?有什么方法可以不保存就上传吗?

这是我的 HTML:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />

    <meta http-equiv="Content-Security-Policy" content="connect-src 'self' http://11.111.11.111/wcf/OCRService.svc/upload 'unsafe-eval' 'unsafe-inline'; default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">

    <title>LoTTo</title>-->

    <!-- LoTTo references -->
    <link href="css/ionicons.css" rel="stylesheet" />
    <link href="css/ionic.css" rel="stylesheet" />
    <link href="css/ng-img-crop.css" rel="stylesheet" />

    <!-- Cordova reference, this is added to your app when it's built. -->
    <script src="scripts/angular.min.js"></script>
    <script src="scripts/ionic.bundle.js"></script>
    <script src="scripts/ng-cordova.min.js"></script>
    <script src="scripts/ng-img-crop.js"></script>
    <script src="cordova.js"></script>
    <script src="scripts/platformOverrides.js"></script>
    <script src="scripts/app.js"></script>
    <script src="scripts/index.js"></script>
    <script src="scripts/ng-file-upload-shim.min.js"></script>
    <script src="scripts/ng-file-upload.min.js"></script>

    <style>
        .cropArea {
            background: #E4E4E4;
            overflow: hidden;
            width: 300px;
            height: 300px;
        }
        .croppedArea {
            background: #E4E4E4;
            overflow: hidden;
            width: 300px;

        }
    </style>
</head>
<body ng-app="starter">
    <ion-header-bar class="bar bar-header bar-light">
        <h1 class="title">LoTTo</h1>
    </ion-header-bar>
    <ion-content ng-controller="ExampleController" padding="true">
        <button class="button button-full button-balanced icon-right ion-ios-camera" ng-click="takePhoto()">
            Resim Çek
        </button>

        <button class="button button-full button-balanced icon-right ion-images" ng-click="choosePhoto()">
            Resim Seç
        </button>
        <center>
            <div class="cropArea">

                <img-crop image="myImage" result-image="myCroppedImage" chargement="'Loading'"
                          area-type="rectangle"
                          area-min-size="50"
                          result-image-format="image/jpeg"
                          result-image-quality="1"
                          result-image-size="{w:300, h:50}"></img-crop>

            </div>
            <div ng-show="myImage !== undefined">Gönderilecek Resim:</div>
            <div class="croppedArea"><img ng-src="{{myCroppedImage}}" ng-show="myImage !== undefined" id="image" /></div>


        </center>
        <button class="button button-full button-balanced icon-right ion-images" ng-click="upload(myCroppedImage);" ng-if="myImage !== undefined">
            Resim Yükle
        </button>

    </ion-content>    
</body>
</html>

这里是 app.js:

angular.module('starter', ['ionic', 'ngCordova', 'ngImgCrop', 'ngFileUpload'])

.controller("ExampleController", ['$scope', '$cordovaCamera', 'Upload', '$timeout', '$cordovaFileTransfer', function ($scope, $cordovaCamera, Upload, $timeout, $cordovaFileTransfer) {

    $scope.takePhoto = function () {
        var options = {
            quality: 100,
            destinationType: Camera.DestinationType.DATA_URL,
            sourceType: Camera.PictureSourceType.CAMERA,
            allowEdit: true,
            encodingType: Camera.EncodingType.JPEG,
            targetWidth: 300,
            targetHeight: 300,
            popoverOptions: CameraPopoverOptions,
            saveToPhotoAlbum: true
        };

        $cordovaCamera.getPicture(options).then(function (imageData) {
            $scope.imgURI = "data:image/jpeg;base64," + imageData;
        }, function (err) {
            console.log(err);
            alert(err);
        });
    }

    $scope.choosePhoto = function () {
        var options = {
            quality: 100,
            destinationType: Camera.DestinationType.DATA_URL,
            sourceType: Camera.PictureSourceType.PHOTOLIBRARY,
            allowEdit: true,
            encodingType: Camera.EncodingType.JPEG,
            targetWidth: 300,
            targetHeight: 300,
            popoverOptions: CameraPopoverOptions,
            saveToPhotoAlbum: false
        };

        $cordovaCamera.getPicture(options).then(function (imageData) {
            $scope.imgURI = "data:image/jpeg;base64," + imageData;
            $scope.myImage = $scope.imgURI;

        }, function (err) {
            console.log(err);
            alert(err);
        });
    }

    $scope.myCroppedImage = '';

    $scope.upload = function (dataUrl) {

    Upload.upload({

        url: 'http://192.168.1.20/wcf/upload',
        data: {
            file: Upload.dataUrltoBlob(dataUrl)
        },

    }).then(function (response) {
        $timeout(function () {
            $scope.result = response.data;
            console.log(response.data);
            alert(response.data);
        });
        console.log(response.data);
    }, function (response) {
        if (response.status > 0) $scope.errorMsg = response.status
            + ': ' + response.data;
        alert(response.status);
    });

}

}]);

编辑 1:如何将 Access-Control-Allow-Origin 添加到我的移动应用程序?

修改 2:更改了上传部分

最好的问候。

【问题讨论】:

    标签: ios angularjs cordova wcf visual-studio-cordova


    【解决方案1】:

    这个例子可能会帮助你: 检查这个jsfiddle:[http://jsfiddle.net/xxo3sk41/1/][1]

    代码:

    var app = angular.module('fileUpload', ['ngFileUpload', 'ngImgCrop']);
    
    app.controller('MyCtrl', ['$scope', 'Upload', '$timeout', function ($scope, Upload, $timeout) {
    $scope.upload = function (dataUrl) {
        Upload.upload({
            url: 'https://angular-file-upload-cors-srv.appspot.com/upload',
            data: {
                file: Upload.dataUrltoBlob(dataUrl)
            },
        }).then(function (response) {
            $timeout(function () {
                $scope.result = response.data;
            });
        }, function (response) {
            if (response.status > 0) $scope.errorMsg = response.status 
                + ': ' + response.data;
        }, function (evt) {
            $scope.progress = parseInt(100.0 * evt.loaded / evt.total);
        });
    }
    }]);
    

    【讨论】:

    • 我收到此错误**Failed to load resource: The request timed out. @Tushar
    • 在家我收到此错误:加载资源失败:服务器响应状态为 405(不允许方法)上传 (0,0)
    • 我正在使用本地 IIS 7.5 并安装了 asp、isapi 等。现在我从响应中得到 0。这是什么意思? IIS 上没有错误日志。如果0表示成功,则上传的文件夹中没有文件。
    • 我会试试这个 [链接] (stackoverflow.com/questions/12458444/…) 并告诉你。
    • @Cenk,这个问题解决了吗?还是找不到解决方案?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-13
    相关资源
    最近更新 更多