当前位置:   article > 正文

uniapp 打开文件管理器上传(H5、微信小程序、android app三端)文件_uniapp打开文件管理器

uniapp打开文件管理器

H5跟安卓APP 手机打开的效果图:

Vue页面:

  1. <template>
  2. <view class="content">
  3. <button @click="uploadFiles">点击上传</button>
  4. </view>
  5. </template>
  6. <script>
  7. export default {
  8. data() {
  9. return {}
  10. },
  11. methods: {
  12. //h5、微信小程序、app上传文件
  13. uploadFiles() {
  14. //#ifdef H5
  15. uni.chooseFile({
  16. count: 1,
  17. extension: ['.doc,.xlsx,.docx'],
  18. success: res => {
  19. uni.showLoading({
  20. title: '导入中...',
  21. mask: true
  22. });
  23. uni.uploadFile({
  24. url: this.$BASE_URL + 'api/uploads/upload',
  25. file: res.tempFiles[0],
  26. name: 'file',
  27. success: (res) => {
  28. },
  29. });
  30. }
  31. });
  32. //#endif
  33. // #ifdef MP-WEIXIN
  34. uni.chooseMessageFile({
  35. count: 1, //默认100
  36. success: res => {
  37. uni.showLoading({
  38. title: '导入中...',
  39. mask: true
  40. });
  41. uni.uploadFile({
  42. url: this.$BASE_URL + 'api/uploads/upload',
  43. filePath: res.tempFiles[0].path,
  44. name: 'file',
  45. success: (res) => {
  46. },
  47. });
  48. }
  49. });
  50. //#endif
  51. // #ifdef APP-VUE
  52. //在这里导入打开安卓app本地文件选择器的封装方法
  53. this.$common.androidChooseFile(res => {
  54. var tempFiles = res;
  55. uni.uploadFile({
  56. url: this.$BASE_URL + 'api/uploads/upload',
  57. filePath: tempFiles,
  58. name: 'file',
  59. success: (res) => {
  60. }
  61. });
  62. },'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
  63. // #endif
  64. },
  65. },
  66. }
  67. </script>
  68. <style scoped></style>

封装的公共方法库utils/common.js: 

  1. var common = {}
  2. //安卓本地文件选择器
  3. common.androidChooseFile = (callback, acceptType) => {
  4. var CODE_REQUEST = 1000;
  5. var main = plus.android.runtimeMainActivity();
  6. if (plus.os.name == 'Android') {
  7. var Intent = plus.android.importClass('android.content.Intent');
  8. var intent = new Intent(Intent.ACTION_GET_CONTENT);
  9. intent.addCategory(Intent.CATEGORY_OPENABLE);
  10. if (acceptType) {
  11. intent.setType(acceptType);
  12. } else {
  13. intent.setType("*/*");
  14. }
  15. main.onActivityResult = (requestCode, resultCode, data) => {
  16. if (requestCode == CODE_REQUEST) {
  17. const uri = data.getData();
  18. plus.android.importClass(uri);
  19. const Build = plus.android.importClass('android.os.Build');
  20. const isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
  21. const DocumentsContract = plus.android.importClass('android.provider.DocumentsContract');
  22. if (isKitKat && DocumentsContract.isDocumentUri(main, uri)) {
  23. if ("com.android.externalstorage.documents" == uri.getAuthority()) {
  24. console.log("6666");
  25. var docId = DocumentsContract.getDocumentId(uri);
  26. var split = docId.split(":");
  27. var type = split[0];
  28. if ("primary" == type) {
  29. var Environment = plus.android.importClass('android.os.Environment');
  30. callback(Environment.getExternalStorageDirectory() + "/" + split[1]);
  31. } else {
  32. var System = plus.android.importClass('java.lang.System');
  33. var sdPath = System.getenv("SECONDARY_STORAGE");
  34. if (sdPath) {
  35. callback(sdPath + "/" + split[1]);
  36. }
  37. }
  38. } else if ("com.android.providers.downloads.documents" == uri.getAuthority()) {
  39. var id = DocumentsContract.getDocumentId(uri);
  40. var ContentUris = plus.android.importClass('android.content.ContentUris');
  41. var contentUri = ContentUris.withAppendedId(
  42. // Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
  43. Uri.parse("content://downloads/public_downloads"), id);
  44. callback(getDataColumn(main, contentUri, null, null));
  45. } else if ("com.android.providers.media.documents" == uri.getAuthority()) {
  46. var docId = DocumentsContract.getDocumentId(uri);
  47. var split = docId.split(":");
  48. console.log(split);
  49. var type = split[0];
  50. console.log(type);
  51. var MediaStore = plus.android.importClass('android.provider.MediaStore');
  52. if ("image" == type) {
  53. contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
  54. } else if ("video" == type) {
  55. contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
  56. } else if ("audio" == type) {
  57. contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
  58. } else {
  59. contentUri = MediaStore.Files.getContentUri("external");
  60. }
  61. console.log(contentUri);
  62. var selection = "_id=?";
  63. var selectionArgs = new Array();
  64. selectionArgs[0] = split[1];
  65. callback(getDataColumn(main, contentUri, selection, selectionArgs));
  66. }
  67. } else if ("content" == uri.getScheme()) {
  68. callback(getDataColumn(main, uri, null, null));
  69. } else if ("file" == uri.getScheme()) {
  70. callback(uri.getPath());
  71. }
  72. }
  73. }
  74. main.startActivityForResult(intent, CODE_REQUEST);
  75. }
  76. function getDataColumn(main, uri, selection, selectionArgs) {
  77. plus.android.importClass(main.getContentResolver());
  78. let cursor = main.getContentResolver().query(uri, ['_data'], selection, selectionArgs,
  79. null);
  80. plus.android.importClass(cursor);
  81. if (cursor != null && cursor.moveToFirst()) {
  82. var column_index = cursor.getColumnIndexOrThrow('_data');
  83. var result = cursor.getString(column_index)
  84. cursor.close();
  85. return result;
  86. }
  87. return null;
  88. }
  89. }
  90. export default common

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/131121
推荐阅读
相关标签
  

闽ICP备14008679号