PhoneGap:将图片上传到本地文件夹

时间:2017-10-13 12:56:02

标签: cordova jquery-mobile phonegap

我使用以下代码在本地文件夹中上传图片。当我使用服务器路径进行文件上传时,它工作正常。但是在使用localhost路径时失败了。它返回'错误:3'。我的目的是将图像存储在本地文件夹中,并在将来同时在线使用它。

我正在使用

  

npm:'3.10.10'cordova:'7.0.1'

JS:

$(document).ready(function() {        
         $('#getPhoto').click(function(e) {
          sessionStorage.removeItem('imagepath');   
          navigator.camera.getPicture(onSuccess, onFail, {
                quality: 50,
                destinationType: Camera.DestinationType.FILE_URI
                //sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY
            });
        });
       });

        function uploadImage(imageURI){
           //var serverURL  = encodeURI("abc.com/xxprojects/upload/upload.php");
           var serverURL  = encodeURI("localhost/xxprojects/upload.php");
           var options    = new FileUploadOptions();
           options.fileKey      = "file";
           options.fileName     = imageURI.substr(imageURI.lastIndexOf('/')+1);
           options.mimeType     = "image/jpeg";
           options.chunkedMode  = false;
           options.headers = {Connection: "close"};
           var ft = new FileTransfer();
           ft.upload(imageURI, serverURL, onUploadSuccess, onUploadError, options);
          }


        function onUploadSuccess(){
            alert('Image Upload Successfully');
        }

        function onUploadError(){
            alert('Error uploading!!');
        }

        function onSuccess(imageURI) {
            var image = document.getElementById('image');
            image.style.display = 'block';
            image.src = imageURI;
            //alert(imageURI);
            uploadImage(imageURI);     
        }




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

HTML:

<div data-role="page" id="cameraPage" data-theme="b">
                                <div data-role="header" data-position="fixed">
                                    <a href="#" class="ui-btn-left" data-rel="back"></a>
                                    <h1>Camera</h1>
                                </div>
                                <div role="main" class="ui-content">
                                    <img style="display:block;width:80px;height:80px;" id="image" src="" />
                                    <br />
                                    <input id="getPhoto" type="button" value="Camera">
                                </div>
                            </div>

PHP:

<?php
//Allow Headers
header('Access-Control-Allow-Origin: *');

// Directory where uploaded images are saved
 $dirname = "upload/"; 
 // If uploading file
 if ($_FILES) {
    print_r($_FILES);
    mkdir ($dirname, 0777, true);
    move_uploaded_file($_FILES["file"]["tmp_name"],$dirname."/".$_FILES["file"]["name"]);
 }

?>

由于

0 个答案:

没有答案