| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- // pages/inedx/IOSDownLoad
-
- <template>
- <view>
- <button v-if="!isWeiXin" class="download-btn" type="default" @click="downLoad()">点击下载{{fileUrl}}</button>
- </view>
- </template>
-
- <script>
- export default {
- data() {
- return {
- isWeiXin: true,
- fileUrl:''
- };
- },
-
- onLoad(options) {
- if(options && options.url){
- this.fileUrl = options.url;
- console.log(this.fileUrl)
- }
-
- this.getIsWxClient();
- },
- methods: {
- // 判断是否是微信浏览器
- getIsWxClient() {
- const ua = navigator.userAgent.toLowerCase()
-
- if (ua.match(/MicroMessenger/i) == 'micromessenger') {
- this.isWeiXin = true;
- uni.showModal({
- title: '提示',
- content: '请点击右上角,在浏览器打开页面。',
- showCancel: false
- });
- } else {
- this.isWeiXin = false
- }
- },
-
- downLoad() {
- console.log('download')
- uni.showLoading({
- title: "正在请求数据"
- });
- uni.request({
- url: '/img'+this.fileUrl, //获取文件流的请求路径
- // url: '/api//img/1.pdf',
- responseType: "arraybuffer",
- success: (response) => {
- uni.hideLoading();
- console.log("response:", response)
- if (!response) {
- uni.showToast({
- title: "下载失败",
- duration: 2000
- });
- }
- let blob = new Blob([response.data]);
- let downloadElement = document.createElement("a");
- let href = window.URL.createObjectURL(blob); //创建下载的链接
- downloadElement.href = href;
- downloadElement.download = this.fileUrl; //下载后文件名
- document.body.appendChild(downloadElement);
- downloadElement.click(); //点击下载
- uni.showToast({
- title: "下载成功",
- duration: 2000
- });
- document.body.removeChild(downloadElement); //下载完成移除元素
- window.URL.revokeObjectURL(href); //释放掉blob对象
-
- }
- })
- }
- },
- }
- </script>
-
- <style lang="scss">
- .download-btn{
- width:50%;
- background:#3273BD;
- color:#fff;
- margin-top:100rpx;
- }
- </style>
|