Remember once, uniapp Android App non-media file upload and download learning record

upload

First of all, to solve the problem of android app file upload, we should consider the selection and upload of files in the app. Since UniApp Input does not support type=file, use the native file path selection API and UniApp’s file upload API.

File selection because I am not familiar with Android native, so I first consider the plug-in market file selection component Tki-file-Manager details click the link to view, here is just to carry the example for easy reading. Then upload to the server using the uniApp official API uni.uploadfile method.

<template> <view class="content"> <button type="primary" @tap="openFile"> </button> <view class="path">{{path}}</view> <tki-file-manager ref="filemanager" @result="resultPath"></tki-file-manager> </view> </template> <script> import tkiFileManager from "@/components/tki-file-manager/tki-file-manager.vue" export default { data() { return { title: '', baseUrl: '', path: '' } }, methods: { openFile(){ this.$refs.filemanager._openFile() }, // resultPath(path){ this.path = path this.sendFile() }, SendFile () {uni.uploadFile({url: baseUrl, filePath: this.path, name: 'file', header: {'Authorization': uni.getStorageSync('lifeData').vuex_token }, success: (uploadFileRes) => { console.log(uploadFileRes.data.id); }}); }, }, components: { tkiFileManager } } </script> <style> .content { width: 100%; overflow: hidden; } .path{ font-size: 14px; word-break:break-all; } </style>Copy the code

download

Because app environment Blob object to download is not available here on the Internet to find the use of to the native API plus. The downloader. CreateDownload method for file.

Async downloadFileFn(id) {// #ifdef app-plus let res = await downloadFile({id: id}); // Let path = res.data.path; // Let path = res.data.path; console.log(path); let url = path; let name = this.file.name; // var name = 'fileName.docx'; / / file name can be saved in the upload, download, when when when there is a single and double quotation marks in the file name should be processing, otherwise an error var dtask = plus. The downloader. CreateDownload (url, {filename: '_downloads/' + name // Use the save path, Download file rename}, function (d, status) {var fileSaveUrl = plus. IO. ConvertLocalFileSystemURL (d.f ilename); plus.runtime.openFile(d.filename); // Select software open file}); dtask.start(); // #endif } }Copy the code

It is just one idea. There is another way to upload online files by using Android native WebView for embedded web pages. However, communication between embedded web pages and app should be considered.

Finally, many of the above articles are on the Internet to find some solutions for some piecing together, this paper is just a record, do not like spray.