<template>
<view class="content">
<button @click="uploadFiles">点击上传</button>
<view>上传前的tempFiles:</view>
<view class="w-[400px] h-[400px] overflow-scroll">{{ tempFiles }}</view>
<view>-----------------</view>
<view>上传得到的数据src:</view>
<view class="w-[400px] h-[400px] overflow-scroll">{{ src }}</view>
</view>
</template>
<script>
import { getAccessToken } from '@/utils/auth'
import common from '@/utils/common.js'
export default {
data() {
return {
src: '',
tempFiles: ''
}
},
methods: {
//h5、微信小程序、app上传文件
uploadFiles() {
//#ifdef H5
uni.chooseFile({
count: 1,
// extension: ['.doc,.xlsx,.docx'],
success: res => {
uni.uploadFile({
// url: this.$BASE_URL + 'api/uploads/upload',
url: 'https://我的服务器/sass/api/admin-api/infra/file/upload',
file: res.tempFiles[0],
name: 'file',
header: {
Authorization: getAccessToken() ? 'Bearer ' + getAccessToken() : ''
},
success: (res) => {
this.src = JSON.parse(res.data)?.data
console.log('res', JSON.parse(res.data)?.data)
},
fail: (err) => {
console.log('err', err)
}
});
}
});
//#endif
// #ifdef MP-WEIXIN
uni.chooseMessageFile({
count: 1, //默认100
success: res => {
// uni.showLoading({
// title: '导入中...',
// mask: true
// });
uni.uploadFile({
// url: this.$BASE_URL + 'api/uploads/upload',
url: 'https:/我的服务器/sass/api/admin-api/infra/file/upload',
filePath: res.tempFiles[0].path,
name: 'file',
header: {
Authorization: getAccessToken() ? 'Bearer ' + getAccessToken() : ''
},
success: (res) => {
this.src = JSON.parse(res.data)?.data
console.log('res', JSON.parse(res.data)?.data)
},
fail: (err) => {
console.log('err', err)
}
});
}
});
//#endif
// #ifdef APP-VUE
//在这里导入打开安卓app本地文件选择器的1封装方1法
common.androidChooseFile(res => {
var tempFiles = res;
this.tempFiles = tempFiles
uni.uploadFile({
// url: this.$BASE_URL + 'api/uploads/upload',
url: 'https://我的服务器/sass/api/admin-api/infra/file/upload',
filePath: tempFiles,
name: 'file',
header: {
Authorization: getAccessToken() ? 'Bearer ' + getAccessToken() : '',
'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'
},
success: (res) => {
console.log('安卓app的res', JSON.parse(res.data)?.data)
this.src = res
// this.src = JSON.parse(res.data)?.data
},
fail: (err) => {
this.src = err
console.log('安卓app的err', err)
}
});
}, 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
// #endif
},
},
}
</script>
<style scoped></style>
var common = {}
//安卓本地文件选择器
common.androidChooseFile = (callback, acceptType) => {
var CODE_REQUEST = 1000;
var main = plus.android.runtimeMainActivity();
if (plus.os.name == 'Android') {
var Intent = plus.android.importClass('android.content.Intent');
var intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
// if (acceptType) {
// intent.setType(acceptType);
// } else {
intent.setType("*/*");
// }
main.onActivityResult = (requestCode, resultCode, data) => {
if (requestCode == CODE_REQUEST) {
const uri = data.getData();
plus.android.importClass(uri);
const Build = plus.android.importClass('android.os.Build');
const isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
const DocumentsContract = plus.android.importClass('android.provider.DocumentsContract');
if (isKitKat && DocumentsContract.isDocumentUri(main, uri)) {
if ("com.android.externalstorage.documents" == uri.getAuthority()) {
console.log("6666");
var docId = DocumentsContract.getDocumentId(uri);
var split = docId.split(":");
var type = split[0];
if ("primary" == type) {
var Environment = plus.android.importClass('android.os.Environment');
callback(Environment.getExternalStorageDirectory() + "/" + split[1]);
} else {
var System = plus.android.importClass('java.lang.System');
var sdPath = System.getenv("SECONDARY_STORAGE");
if (sdPath) {
callback(sdPath + "/" + split[1]);
}
}
} else if ("com.android.providers.downloads.documents" == uri.getAuthority()) {
var id = DocumentsContract.getDocumentId(uri);
var ContentUris = plus.android.importClass('android.content.ContentUris');
var contentUri = ContentUris.withAppendedId(
// Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
Uri.parse("content://downloads/public_downloads"), id);
callback(getDataColumn(main, contentUri, null, null));
} else if ("com.android.providers.media.documents" == uri.getAuthority()) {
var docId = DocumentsContract.getDocumentId(uri);
var split = docId.split(":");
console.log(split);
var type = split[0];
console.log(type);
var MediaStore = plus.android.importClass('android.provider.MediaStore');
if ("image" == type) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video" == type) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if ("audio" == type) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
} else {
contentUri = MediaStore.Files.getContentUri("external");
}
console.log(contentUri);
var selection = "_id=?";
var selectionArgs = new Array();
selectionArgs[0] = split[1];
callback(getDataColumn(main, contentUri, selection, selectionArgs));
}
} else if ("content" == uri.getScheme()) {
callback(getDataColumn(main, uri, null, null));
} else if ("file" == uri.getScheme()) {
callback(uri.getPath());
}
}
}
main.startActivityForResult(intent, CODE_REQUEST);
}
function getDataColumn(main, uri, selection, selectionArgs) {
plus.android.importClass(main.getContentResolver());
let cursor = main.getContentResolver().query(uri, ['_data'], selection, selectionArgs,
null);
plus.android.importClass(cursor);
if (cursor != null && cursor.moveToFirst()) {
var column_index = cursor.getColumnIndexOrThrow('_data');
var result = cursor.getString(column_index)
cursor.close();
return result;
}
return null;
}
}
export default common
2 个回复
DCloud_App_Array
云端打包后是什么现象?调用上传API有返回什么错误信息吗?
喜欢技术的前端 - QQ---445849201
可以打自定义基座包调试