WP7 - PhoneGap FileTransfer返回Null错误代码

时间:2012-10-29 16:20:11

标签: windows-phone-7 cordova file-transfer

我第一次在WP7中尝试PhoneGap / Cordova 2.1.0,所以我是新手。

我应该做的是通过相机拍摄照片并将其上传到服务器。

这是我的代码:

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width, height=device-height, initial-scale=1.0, maximum-scale=1.0, user-scalable=no;" />
    <meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
    <title>PhoneGap WP7</title>
    <script type="text/javascript" charset="utf-8" src="cordova-2.1.0.js"></script>

    <script type="text/javascript">

        document.addEventListener("deviceready", onDeviceReady, false);

        function onDeviceReady() { }

        function capturePhoto() 
        {
            // Take picture using device camera and retrieve image
            navigator.camera.getPicture(
                onPhotoDataSuccess, 
                onFail, 
                { 
                    quality: 50, 
                    destinationType: Camera.DestinationType.DATA_URL 
                }
            );
        }

        function onPhotoDataSuccess(imageData) 
        {
            var options = new FileUploadOptions();
            options.fileKey = "file";
            options.fileName = imageData.substr(imageData.lastIndexOf('/') + 1);
            options.mimeType="image/jpeg";

            var params = new Object();

            options.params = params;

            var ft = new FileTransfer();
            ft.upload(imageData, "http://mysite.com/upload.php", win, fail, options);
        }

        function onFail(message) 
        {
            navigator.notification.alert('Failed because: ' + message);
        }


        var win = function(r) 
        {
            navigator.notification.alert(r.responseCode + " - " + r.response + " - " + r.bytesSent);
        }

        var fail = function(error) 
        {
            navigator.notification.alert(eval(error));
        }

    </script>
</head>
<body>
    <h1>PhoneGap Photo Demo</h1>
    <button onclick="capturePhoto();">Capture a Photo</button>
</body>
</html>

当我尝试这个时,上传不起作用,我得到一个空的错误对象:

{ "code":null, "source":null, "target":null, "http_status":null }

一些注意事项:

我跟踪this trick因为WP7中缺少白名单。

php代码不是由我编写的,我会在asp.net中编写,如果有人可以提供asp.net webservice的工作示例,我将非常感激。

感谢。

修改

我调试了FileTransfer类,我在JsonHelper类中遇到错误:

using (MemoryStream mem = new MemoryStream(Encoding.UTF8.GetBytes(json)))
{
   result = deserializer.ReadObject(mem);
}

错误是:InvalidCastException

1 个答案:

答案 0 :(得分:0)

修改

尝试替换

uploadOptions = JSON.JsonHelper.Deserialize<UploadOptions[]>(options)[0];

使用以下

options = JSON.JsonHelper.Deserialize<string[]>(options)[0];
uploadOptions = JSON.JsonHelper.Deserialize<UploadOptions>(options);

原始答案

解决此类问题的最简单方法是添加对Cordova库源代码的引用,而不是编译WP7CordovaClassLib.dll

cordova-2.1.0-incubating-src \ cordova-2.1.0 \ incubator-cordova-wp7 \ framework \ WP7CordovaClassLib.csproj

http://www.apache.org/dist/incubator/cordova/

然后将断点添加到standalone \ cordovalib \ Commands \ FileTransfer.cs

    /// <summary>
    /// sends a file to a server
    /// </summary>
    /// <param name="options">Upload options</param>
    public void upload(string options)
    {
        Debug.WriteLine("options = " + options);
        options = options.Replace("{}", "null");

听起来很复杂,但根据我的经验,逐行调试是解决Apache Cordova在许多情况下出现什么问题的最简单方法。