0

我尝试将图像从 android 应用程序上传到服务器。我正在使用 HTML5 和电话间隙。我正在使用 WCF 将图像从 android 应用程序发送到远程服务器。当我尝试使用我的应用上传时,它会上传一个文件,但文件的大小为 0,所以里面没有任何内容。(我的 WCF 工作正常,只是我知道问题出在我的 android 应用程序上)

这是我的上传代码:

         function uploadPicture() {
        // Get URI of picture to upload  
        var img = document.getElementById('camera_image');
        var imageURI = img.src;

        if (!imageURI || (img.style.display == "none")) {

            document.getElementById('camera_status').innerHTML = "Take picture or select picture from library first.";
            return;
        }
        // Verify server has been entered  
        server = document.getElementById('serverUrl').value;
        if (server) {
            // Specify transfer options    
            var options = new FileUploadOptions();
            options.fileKey = "file";
            options.fileName = imageURI.substr(imageURI.lastIndexOf('/') + 1);
            options.mimeType = "image/jpeg";
            options.chunkedMode = false;
            // Transfer picture to server
            var ft = new FileTransfer();
            ft.upload(imageURI, server, function(r) {
                document.getElementById('camera_status').innerHTML = "Upload successful: " + r.bytesSent + " bytes uploaded.";
            }, function(error) { document.getElementById('camera_status').innerHTML = "Upload failed: Code = " + error.code; },
                                                              options);

        } 
    }
4

1 回答 1

0

尝试使用:

options.mimeType = "multipart/form-data";

并尝试设置一些参数。我发布了一个对我有用的示例代码:

var imageFile=$("#largeImage").attr('src');

                        if(imageFile=="")
                            alert("No image to upload.");
                        else{
                        var ft,options;
                        options = new FileUploadOptions();
                        options.fileKey = "profile_image";
                        options.fileName=name;
                        options.mimeType = "multipart/form-data";

                          params = {
                            val1: "some value",
                            val2: "some other value"
                          };
                          options.params = params;
                        ft = new FileTransfer();
                        ft.upload(imageFile, uploadUrl, success, fail, options);
                        }
                    }

我正在使用 java servlet 来处理上传的图像。

于 2013-05-17T05:54:40.707 回答