Cordova相机将空文件发送到ASP.net服务器

时间:2015-09-23 07:38:18

标签: asp.net cordova

我正在尝试将从cordova相机拍摄的图像发送到我的服务器。问题是服务器为图像返回null。我试图相信AllHost为cordovaCamera但无济于事,可能是什么问题?谢谢!

Cordova Side:

.controller('MapCtrl',function($scope, $cordovaCamera, $cordovaGeolocation, StoreService, dataFactory){

$scope.getPhoto = function(){

    //--------------------- get geolocation lat long --------//
        var posOptions = {timeout: 10000, enableHighAccuracy: false};
          $cordovaGeolocation
            .getCurrentPosition(posOptions)
            .then(function (position) {
              var latitude  = position.coords.latitude
              var longitude = position.coords.longitude

              console.log("photo lat: "+latitude+" photo long: "+longitude);
            }, function(err) {
              // error
            });
    //--------------------- get geolocation lat long --------//

    document.addEventListener("deviceready", function () {

        var options = {
          quality: 100,
          //destinationType: Camera.DestinationType.DATA_URL,
          //sourceType: Camera.PictureSourceType.CAMERA,
          //allowEdit: true,
          //encodingType: Camera.EncodingType.JPEG,
          targetWidth: 320,
          targetHeight: 320,
          //popoverOptions: CameraPopoverOptions,
          saveToPhotoAlbum: false
        };

        $cordovaCamera.getPicture(options).then(function(imageData) {

          console.log(imageData);




        //----- get datetime----//
        var d = new Date();
        console.log("datetime: "+d);
        //----- get datetime----//


        //--------------------- upload image into server --------//

var server = 'http://192.168.2.50/webservice.asmx/SaveImage';
        var trustAllHosts = true;

        var ftOptions = new FileUploadOptions();
        ftOptions.fileKey = 'file';
        ftOptions.fileName = imageData.substr(imageData.lastIndexOf('/') + 1);
        ftOptions.mimeType = 'image/jpeg';
        ftOptions.httpMethod = 'POST';

        var fileTransfer = new FileTransfer();


        fileTransfer.upload(imageData, encodeURI(server), win, fail, ftOptions);

        function win(r) {
            alert("Photo uploaded");
            console.log("Code = " + r.responseCode);
            console.log("Response = " + r.response);
            console.log("Sent = " + r.bytesSent);
        }

        function fail(error) {
            alert("An error has occurred: Code = " + error.code);
    console.log(error);
            console.log("upload error source " + error.source);
            console.log("upload error target " + error.target);
        }
        //--------------------- upload image into server --------//

        }, function(err) {
          // error
          alert('Failed because: ' + err);
        });

    }, false);

}
})

注意:ImageData返回有效的URL(文件:///storage/emulated/0/Android/data/com.ionicframework.ionicnavigationtest216511/cache/1442991645110.jpg)

  

r.responseCode返回200(假设为http 200 OK状态)

     

r.response让我回来

<?xml version="1.0" encoding="utf-8"?>
<string xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true" xmlns="http://www.xxx.<hidden>.<hidden>" />
  

r.bytesent给我回报32880

WebService部分:

  public string SaveImage()
    {

        HttpPostedFile file = HttpContext.Current.Request.Files["recFile"];

        if (file == null)
            return null;

     //   string targetFilePath = "c:\\deposit\\" + file.FileName;
       // file.SaveAs(targetFilePath);
        return file.FileName.ToString();
    } 

注意:当我测试(file == null)时,它返回true。任何建议都将不胜感激。

更新1:更改图像的大小(将质量降低到10并将字节发送到112)也没有解决问题。

2 个答案:

答案 0 :(得分:0)

你应该以这种方式使用相机插件,请仔细阅读评论。

$pdo = new PDO(DSN, USER, PASSWORD,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));

另见文档here

答案 1 :(得分:0)

我不知道asp,但我认为这段代码是用于获取带有recFile名称的文件

public string SaveImage()
    {

        HttpPostedFile file = HttpContext.Current.Request.Files["recFile"];

        if (file == null)
            return null;

     //   string targetFilePath = "c:\\deposit\\" + file.FileName;
       // file.SaveAs(targetFilePath);
        return file.FileName.ToString();
    } 

但是在FileUploadOptions上,您将fileKey作为文件发送

var ftOptions = new FileUploadOptions();
        ftOptions.fileKey = 'file';
        ftOptions.fileName = imageData.substr(imageData.lastIndexOf('/') + 1);
        ftOptions.mimeType = 'image/jpeg';
        ftOptions.httpMethod = 'POST';

因此,将fileKey更改为recFile或更改asp代码转到&#34;文件&#34;