0

我想将相机图片存储在服务器上,所以我有 PhoneGap 的用户文件上传 API,但没有运气。

**我的目标平台是 Windows Phone 7 和 Android。在 Windows Phone 上我收到“连接错误”,在 Android 上我收到 File Not found 错误。我提到了这个链接 从 phonegap 应用程序上传图片到 WCF 服务 **

这是我的代码

 $(document).on({
            'pageinit': function (e) {
                $(".divTakePhoto").on('click', function (event) {
                     event.preventDefault();
                     getPicture();
                });
              },
    }, "#pgHome");

function getPicture(source) {
    navigator.camera.getPicture(onCaptureSuccess, onCaptureFail,
                        {
                            quality: 10,
                            destinationType: navigator.camera.DestinationType.FILE_URI,
                            sourceType: navigator.camera.PictureSourceType.CAMERA
                        });
}

function onCaptureFail(message) {
    alert('Failed because: ' + message);
};

function onCaptureSuccess(imageURI) {
    var options = new FileUploadOptions();
    options.chunkedMode = false;
    options.fileKey = "recFile";
    var imagefilename = imageURI;
    options.fileName = imagefilename;
    options.mimeType = "image/jpeg";
    var ft = new FileTransfer();
    ft.upload(imageURI, "http://myserver/MyAppService.svc/SaveImage", win, fail, options);
}


function resolveOnSuccess(entry) {
    fileuri = entry.toURL();
    //console.log(fileuri);
}

function fsFail(message) {
    alert(JSON.stringify(message));
}

function win(r) {
    alert("Sent = " + r.bytesSent);
}

function fail(error) {
    switch (error.code) {
        case FileTransferError.FILE_NOT_FOUND_ERR:
            alert("Photo file not found");
            break;
        case FileTransferError.INVALID_URL_ERR:
            alert("Bad Photo URL");
            break;
        case FileTransferError.CONNECTION_ERR:
            alert("Connection error");
            break;
    }
    alert("An error has occurred: Code = " + error.code);
}

如果我缺少任何东西,请帮助我。我还尝试通过 $.ajax() 发布图像这是将图像发布到 http 处理程序的代码,但它显示解析错误。

function uploadPhoto(imageURI) {
    $.ajax({
        type: "POST",
        url: "http://myserver/ImageHandler.ashx",
        data: {                
            imgData: imageURI
        },
        success: function (data) {
            alert(data);
            var arr = new Array();
            var responseData = data;
            arr = (data.split("."));
            if (arr[1].toString() != '') {

            }
            else
                alert("Uploaded! " + data);
            $.mobile.changePage("home.html");
            var imageData = responseData.split("-");
        },
        error: function (request, error) {
            alert("Error! " + error);
        }
    });
}
4

0 回答 0