Access sd card in android for uploading a file to my php server using phonegap

本小妞迷上赌 提交于 2019-12-02 19:37:28

问题


I want to go to select a file from sdcard and upload it to server. is it possible to access the sdcard in android via phonegap as how we are picking a image from gallery and uploading. I went through samples but all are specifying the file name also like eg: mnt/sdcard/read.txt. But i want to goto only sdcard so that user can select his own file is it possible to do.


回答1:


U can easily do that its very easy

    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccessUpload, fail);

    function onFileSystemSuccessUpload(fileSystem) {
     // get directory entry through root and access all the folders
             var directoryReader = fileSystem.root.createReader();

    // Get a list of all the entries in the directory
    directoryReader.readEntries(successReader,fail); 

          }

      function successReader(entries) {
        var i;
        for (i=0; i<entries.length; i++) {
           //alert(entries[i].name);
           if(entries[i].isDirectory==true)
           {
             var directoryReaderIn = entries[i].createReader();
            directoryReaderIn.readEntries(successReader,fail); 

           }

            if(entries[i].isFile==true)
             {
          entries[i].file(uploadFile, fail);
           }
        }
    }; 

 function uploadFile(file) {
var target=""; //the url to upload on server
     var ft = new FileTransfer(),path = "file://"+ file.fullPath,name = file.name;
                ft.upload(path, target, win, fail, { fileName: name });
               // var ft = new FileTransfer();
              //ft.upload(file.fullPath, target, win, fail, options);


            function win(r) {
                alert("Code = " + r.responseCode);
               alert("Response = " + r.response);
                alert("Sent = " + r.bytesSent);
            }

            function fail(error) {
                alert("An error has occurred: Code = " + error.code);
            }
}


来源:https://stackoverflow.com/questions/9953483/access-sd-card-in-android-for-uploading-a-file-to-my-php-server-using-phonegap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!