【问题标题】:Uploading Images using phonegap使用 phonegap 上传图片
【发布时间】:2014-06-02 10:11:02
【问题描述】:

我是 phonegap 新手,在上传图片到服务器时遇到了很多麻烦。

我通过浏览和使用相机将单个图像上传到服务器。 我想使用相机上传多张图片,同时浏览图库。

我该怎么做?

【问题讨论】:

    标签: cordova sencha-touch sencha-touch-2


    【解决方案1】:

    检查这个答案https://stackoverflow.com/a/23282229/1682392也许它可以帮助你。

    <!DOCTYPE html>
    <html>
      <head>
        <title>Submit form</title>
    
        <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
        <script type="text/javascript" charset="utf-8">
    
        var pictureSource;   // picture source
        var destinationType; // sets the format of returned value
    
        // Wait for device API libraries to load
        //
        document.addEventListener("deviceready",onDeviceReady,false);
    
        // device APIs are available
        //
        function onDeviceReady() {
            pictureSource = navigator.camera.PictureSourceType;
            destinationType = navigator.camera.DestinationType;
        }
    
    
        // Called when a photo is successfully retrieved
        //
        function onPhotoURISuccess(imageURI) {
    
            // Show the selected image
            var smallImage = document.getElementById('smallImage');
            smallImage.style.display = 'block';
            smallImage.src = imageURI;
        }
    
    
        // A button will call this function
        //
        function getPhoto(source) {
          // Retrieve image file location from specified source
          navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 50,
            destinationType: destinationType.FILE_URI,
            sourceType: source });
        }
    
        function uploadPhoto() {
    
            //selected photo URI is in the src attribute (we set this on getPhoto)
            var imageURI = document.getElementById('smallImage').getAttribute("src");
            if (!imageURI) {
                alert('Please select an image first.');
                return;
            }
    
            //set upload options
            var options = new FileUploadOptions();
            options.fileKey = "file";
            options.fileName = imageURI.substr(imageURI.lastIndexOf('/')+1);
            options.mimeType = "image/jpeg";
    
            options.params = {
                firstname: document.getElementById("firstname").value,
                lastname: document.getElementById("lastname").value,
                workplace: document.getElementById("workplace").value
            }
    
            var ft = new FileTransfer();
            ft.upload(imageURI, encodeURI("http://some.server.com/upload.php"), win, fail, options);
        }
    
        // Called if something bad happens.
        //
        function onFail(message) {
          console.log('Failed because: ' + message);
        }
    
        function win(r) {
            console.log("Code = " + r.responseCode);
            console.log("Response = " + r.response);
            //alert("Response =" + r.response);
            console.log("Sent = " + r.bytesSent);
        }
    
        function fail(error) {
            alert("An error has occurred: Code = " + error.code);
            console.log("upload error source " + error.source);
            console.log("upload error target " + error.target);
        }
    
        </script>
      </head>
      <body>
        <form id="regform">
            <button onclick="getPhoto(pictureSource.PHOTOLIBRARY);">Select Photo:</button><br>
            <img style="display:none;width:60px;height:60px;" id="smallImage" src="" />
    
            First Name: <input type="text" id="firstname" name="firstname"><br>
            Last Name: <input type="text" id="lastname" name="lastname"><br>
            Work Place: <input type="text" id="workplace" name="workPlace"><br>
            <input type="button" id="btnSubmit" value="Submit" onclick="uploadPhoto();">
        </form>
      </body>
    </html>
    

    如果您想从库中获取图像而不是使用相机,请使用答案 getPhoto(pictureSource.PHOTOLIBRARY); 中描述的函数。 但是,对于选择各种图像,您将面临一些麻烦,您可以在此处看到https://issues.apache.org/jira/browse/CB-1215

    您必须为此编写一个自定义插件,或者查找是否有人已经制作了一个http://plugins.cordova.io/

    【讨论】:

      猜你喜欢
      • 2013-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-16
      • 2015-02-03
      • 2023-03-07
      • 2012-05-30
      • 2014-08-30
      相关资源
      最近更新 更多