1. 相關(guān)插件
文件管理器
cordova plugin add cordova-plugin-filechooser
路徑轉(zhuǎn)換
cordova plugin add cordova-plugin-filepath
文件讀寫
cordova plugin add cordova-plugin-file
上傳文件
cordova plugin add cordova-plugin-file-transfer
打開文件
cordova plugin add https://github.com/JuanjoPP/cordova-plugin-file-opener2
gradle版本跟crosswalk產(chǎn)生沖突,安裝兼容插件
cordova plugin add cordova-android-support-gradle-release --variable ANDROID_SUPPORT_VERSION=24.+
2. 返回文件路徑的兩種形式
// 安卓 7.0以上文件路徑以 'content' 開頭挨决,7.0 下以 'file' 開頭
// 轉(zhuǎn)換前的路徑
content://media/external/file/306905
// 轉(zhuǎn)換后的路徑
file:///storage/emulated/0/測試文件.docx
3. 代碼部分
server.js
// 轉(zhuǎn)換文件路徑
.service('resolveFileService', function (openFileService, $rootScope) {
// 打開文件
function openFile(path) {
var fileType = path.substring(path.lastIndexOf(".") + 1, path.length); // 文件名后綴
openFileService.openFile(path, fileType);
};
// 文件路徑轉(zhuǎn)換,統(tǒng)一成file開頭的文件路徑
function resolveUrl(newUrl, type) {
window.FilePath.resolveNativePath(newUrl, function (path) {
if (type === 'open') {
openFile(path);
} else if (type === 'upload') {
return path;
}
}, function (err) {
console.log(err);
});
};
/**
* 打開文件管理器,選擇上傳文件
* type 區(qū)分類型 'open' 打開文件芽世,'upload' 上傳文件
* url 選擇文件后返回的路徑
*/
this.fileChooser = function (type) {
return fileChooser.open(function (url) {
var path = decodeURI(url);
var isExist = path.indexOf('content'); // 安卓 7.0以上文件路徑以 'content' 開頭齐佳,7.0 下以 'file' 開頭
if (isExist != -1) {
resolveUrl(path, type);
} else {
if (type === 'open') {
openFile(path);
} else if (type === 'upload') {
$rootScope.uploadSendEmail(path); // 上傳文件
}
}
}, function (err) {
console.log(err);
});
}
})
// 打開文件
.service('openFileService', function ($cordovaFileOpener2) {
function getFileMimeType(fileType) {
var mimeType = '';
switch (fileType) {
case 'txt':
mimeType = 'text/plain';
break;
case 'docx':
mimeType = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
break;
case 'doc':
mimeType = 'application/msword';
break;
case 'pptx':
mimeType = 'application/vnd.openxmlformats-officedocument.presentationml.presentation';
break;
case 'ppt':
mimeType = 'application/vnd.ms-powerpoint';
break;
case 'xlsx':
mimeType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
break;
case 'xls':
mimeType = 'application/vnd.ms-excel';
break;
case 'zip':
mimeType = 'application/x-zip-compressed';
break;
case 'rar':
mimeType = 'application/octet-stream';
break;
case 'pdf':
mimeType = 'application/pdf';
break;
case 'apk':
mimeType = 'application/vnd.android.package-archive';
break;
case 'jpg':
mimeType = 'image/jpeg';
break;
case 'png':
mimeType = 'image/png';
break;
default:
mimeType = 'application/' + fileType;
break;
}
return mimeType;
}
// 打開文件
this.openFile = function (path, type) {
$cordovaFileOpener2.open(path, getFileMimeType(type)).then(function (res) {
// 成功
console.log(res);
}, function (err) {
console.log(err);
// 錯(cuò)誤
});
}
})
controller.js
// 上傳文件
$scope.postUploadFile = function () {
resolveFileService.fileChooser('upload'); // 'open' 打開文件重付,'upload' 上傳文件
};
//圖片上傳
$rootScope.uploadSendEmail = function (path) {
console.log(path);
var filename = path.split("/0/").pop();
var mimeType = path.split(".").pop();
var targetPath = cordova.file.externalRootDirectory + filename; //APP下載存放的路徑,可以使用cordova file插件進(jìn)行相關(guān)配置
var url = encodeURI(intraHost + "/OA/postUploadFile");
var trustHosts = true;
var options = {
// fileKey: 'file',
mimeType: mimeType,
fileName: filename,
};
$ionicLoading.show({
template: "正在上傳..."
});
$cordovaFileTransfer.upload(url, targetPath, options, trustHosts)
.then(function (result) {
console.log(result);
$ionicLoading.hide();
$cordovaToast.showShortBottom('上傳成功');
}, function (err) {
console.log(err);
$ionicLoading.hide();
$cordovaToast.showShortBottom('上傳失敗');
}, function (progress) {
$timeout(function () {
var downloadProgress = (progress.loaded / progress.total) * 100;
$ionicLoading.show({
template: "已上傳:" + Math.floor(downloadProgress) + "%",
});
if (downloadProgress > 99) {
$ionicLoading.show({
template: "已上傳:100%",
});
}
})
});
};