简体   繁体   English

将图像从android应用上传到服务器

[英]upload an image from android app to the server

I try to upload an image from android app to the server. 我尝试将图像从android应用程序上传到服务器。 I am using HTML5 and phone gap. 我正在使用HTML5和手机间隙。 I am sending image from android app to remote server using WCF. 我正在使用WCF将图像从android应用发送到远程服务器。 when I try to upload using my app, it uploads a file but file is 0 size so nothing is in it. 当我尝试使用我的应用上传时,它会上传一个文件,但文件大小为0,因此其中没有任何内容。 (my WCF works fine just I know problem is with my android app) (我的WCF正常工作,只是我知道问题出在我的Android应用程序上)

this is my upload code: 这是我的上传代码:

         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);

        } 
    }

Try using: 尝试使用:

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

And try to set some parameters. 并尝试设置一些参数。 I am posting a sample code which is working for me: 我正在发布适合我的示例代码:

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);
                        }
                    }

I am using java servlet for handling the uploaded image. 我正在使用Java Servlet处理上传的图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM