更新记录
1.0.0(2020-07-17)
下载此版本
// #ifndef H5
var x = document.createElement('input');
x.setAttribute('type', 'file');
x.click();
x.onchange = function() {
console.log(this.files[0]);
};
// #endif
// #ifdef APP-PLUS
var REQUESTCODE = 1;
var main = plus.android.runtimeMainActivity();
var Intent = plus.android.importClass('android.content.Intent');
var intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType('*/*'); //设置类型,任意类型
//intent.setType("image/*");
//intent.setType("audio/*"); //选择音频
//intent.setType("video/*"); //选择视频 (mp4 3gp 是android支持的视频格式)
intent.addCategory(Intent.CATEGORY_OPENABLE);
main.startActivityForResult(intent, REQUESTCODE);
main.onActivityResult = function(requestCode, resultCode, data) {
if (REQUESTCODE == requestCode) {
var context = main;
plus.android.importClass(data);
// 获得文件路径
var fileData = data.getData();
var path = plus.android.invoke(fileData, 'getPath');
console.log('path:' + path);
// 判断文件类型
var resolver = context.getContentResolver();
var fileType = plus.android.invoke(resolver, 'getType', fileData);
console.log('fileType:' + fileType);
}
};
// #endif
平台兼容性
<template>
<view class="content"><button type="default" @tap="uploadFilesFun">上传文件</button></view>
</template>
<script>
export default {
data() {
return {
title: 'Hello'
};
},
onLoad() {},
methods: {
uploadFilesFun() {
// #ifdef APP-PLUS
var REQUESTCODE = 1;
var main = plus.android.runtimeMainActivity();
var Intent = plus.android.importClass('android.content.Intent');
var intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType('*/*'); //设置类型,任意类型
//intent.setType("image/*");
//intent.setType("audio/*"); //选择音频
//intent.setType("video/*"); //选择视频 (mp4 3gp 是android支持的视频格式)
intent.addCategory(Intent.CATEGORY_OPENABLE);
main.startActivityForResult(intent, REQUESTCODE);
main.onActivityResult = function(requestCode, resultCode, data) {
if (REQUESTCODE == requestCode) {
var context = main;
plus.android.importClass(data);
// 获得文件路径
var fileData = data.getData();
var path = plus.android.invoke(fileData, 'getPath');
console.log('path:' + path);
// 判断文件类型
var resolver = context.getContentResolver();
var fileType = plus.android.invoke(resolver, 'getType', fileData);
console.log('fileType:' + fileType);
}
};
// #endif
// #ifdef H5
var x = document.createElement('input');
x.setAttribute('type', 'file');
x.click();
x.onchange = function() {
console.log(this.files[0]);
};
// #endif
}
}
};
</script>
<style>
.content {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.logo {
height: 200rpx;
width: 200rpx;
margin-top: 200rpx;
margin-left: auto;
margin-right: auto;
margin-bottom: 50rpx;
}
.text-area {
display: flex;
justify-content: center;
}
.title {
font-size: 36rpx;
color: #8f8f94;
}
</style>