将图像从Android应用程序上传到服务器

时间:2013-05-16 21:09:51

标签: android cordova

我尝试将图像从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);

        } 
    }

1 个答案:

答案 0 :(得分: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来处理上传的图像。