本文介绍了在Android中访问SD卡上传文件到我的PHP服务器使用phonegap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从SD卡中选择一个文件并上传到服务器。是否有可能通过手机访问android中的SD卡作为我们如何从图库中上传图片和上传。我通过示例,但都是指定文件的名称也像例如:mnt / sdcard / read.txt。但我只想转到SD卡,以便用户可以选择自己的文件是可以做到的。解决方案
U很容易做到这一点非常简单
window.requestFileSystem(LocalFileSystem.PERSISTENT,0,onFileSystemSuccessUpload,失败);
函数onFileSystemSuccessUpload(fileSystem){
//通过root获取目录条目并访问所有文件夹
var directoryReader = fileSystem.root.createReader();
//获取目录中所有条目的列表
directoryReader.readEntries(successReader,fail);
}
函数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);
$ b if(entries [i] .isFile == true)
{
条目[i] .file(uploadFile,fail);
}
}
};
function uploadFile(file){
var target =; //上传到服务器上的url
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);
函数win(r){
alert(Code =+ r.responseCode);
alert(Response =+ r.response);
alert(Sent =+ r.bytesSent);
函数失败(错误){
alert(发生错误:Code =+ error.code);
}
}
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.
解决方案
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); } }
这篇关于在Android中访问SD卡上传文件到我的PHP服务器使用phonegap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-03 12:25