首先小程序端不支持上传非图像视频文件。
然后App和H5端,参考: https://uniapp.dcloud.io/api/media/file
H5端在2.9.9以前,可以使用如下方法:
-
使用web-view组件
使用 web-view 组件,在 web-view 组件内可以使用 input 元素进行选择,使用表单或者 xhr 上传。 -
使用 js 创建 input 元素进行选择
使用 xhr 上传(或者转 base64、Object-URL 使用 uni.uploadFile 上传),如果对 dom 不熟悉建议使用通用方法。
<template>
<view>
<view ref="input" class="input"></view>
</view>
</template>
<script>
export default {
mounted() {
var input = document.createElement('input')
input.type = 'file'
input.onchange = (event) => {
console.log(event)
}
this.$refs.input.$el.appendChild(input)
}
}
</script>
上传文件,推荐使用uniCloud的云存储和cdn,免费!且提供方便安全的客户端直传,详见:https://uniapp.dcloud.io/uniCloud/storage?id=uploadfile