> app中圖片上傳是經常用到的功能
[TOC]
## 圖片上傳
> 使用 [文件上傳:uni.uploadFile()](https://uniapp.dcloud.io/api/request/network-file?id=uploadfile)方式
~~~
<template>
<view class="content">
<progress :percent="percent" strock-width="10"></progress>
<button type="primary" @tap="upload">chooseImg</button>
</view>
</template>
<script>
// 注冊一個進度條
var _self;
export default {
data() {
return {
percent:0
}
},
onLoad() {
_self = this;
},
methods: {
upload:function(){
uni.chooseImage({
count: 1,
sizeType:['copressed'],
success(res) {
// console.log(res.tempFilePaths) // 如果設置可選多張,這里可打印多張圖地址
//因為有一張圖片, 輸出下標[0], 直接輸出地址
var imgFiles = res.tempFilePaths[0]
console.log(imgFiles)
// 上傳圖片
// 做成一個上傳對象
var uper = uni.uploadFile({
// 需要上傳的地址
url:'http://demo.hcoder.net/index.php?c=uperTest',
// filePath 需要上傳的文件
filePath: imgFiles,
name: 'file',
success(res1) {
// 顯示上傳信息
console.log(res1)
}
});
// onProgressUpdate 上傳對象更新的方法
uper.onProgressUpdate(function(res){
// 進度條等于 上傳到的進度
_self.percent = res.progress
console.log('上傳進度' + res.progress)
console.log('已經上傳的數據長度' + res.totalBytesSent)
console.log('預期需要上傳的數據總長度' + res.totalBytesExpectedToSend)
})
}
})
}
}
}
</script>
~~~
## 頭像上傳
> 比如用戶資料修改,其展現方式與以上不同
> 可參考:https://www.cnblogs.com/wo1ow1ow1/p/11730220.html
## 圖片緩存
> 使用 [文件下載:uni.downloadFile()](https://uniapp.dcloud.io/api/request/network-file?id=downloadfile)方式
~~~
<template>
<view class="content">
<image :src="imgUrl4display"></image>
</view>
</template>
<script>
var _self;
export default {
data() {
return {
imgUrl4display: '',
imgRemoteUrl: 'https://img-cdn-qiniu.dcloud.net.cn/uniapp/images/shuijiao.jpg'
}
},
onLoad() {
_self = this;
let keyName = 'image_storage';
uni.getStorage({
key: keyName,
success: function(res) {
console.log('get storage success...');
_self.displayImg(res.data);
},
fail: function(res) {
console.log('fail 2 download...');
// 本地沒有緩存 需要下載
uni.downloadFile({
url: _self.imgRemoteUrl,
success: (res) => {
console.log(res)
if (res.statusCode === 200) {
_self.displayImg(res.tempFilePath);
uni.setStorage({
key: keyName,
data: res.tempFilePath,
success: function() {
}
})
}
}
});
}
})
},
methods: {
displayImg: function(data) {
_self.imgUrl4display = data;
}
}
}
</script>
~~~
- 基礎知識
- UNI核心介紹
- flex布局
- 生命周期
- 全局方法
- 組件定義
- 自定義組件
- 全局組件
- 組件之間的數據傳輸
- 條件編譯
- 自定義頭部
- 節點信息 (SelectorQuery)
- vuejs基礎語法
- 頁面跳轉以及參數傳遞
- 事件的監聽注冊以及觸發
- css3動畫
- block的妙用
- mixin (混入)
- uniapp快捷鍵
- vuex狀態管理
- 實用功能
- 獲取服務提供商
- 啟動頁 / 啟動界面
- 引導頁
- tabbar配置
- 頭部導航欄基礎設置
- 上拉下拉(刷新/加載)
- 第三方登錄
- 第三方分享
- 推送通知 之 unipush
- scroll-view雙聯動
- 配置iOS通用鏈接(Universal Links)
- 本地緩存操作
- 升級/更新方案
- 熱更新
- 圖片上傳
- 搜索頁實現
- canvas繪圖助手
- 地圖定位
- 第三方支付————todo
- 分類輪播
- 清除應用緩存
- uniapp與webview的實時通訊
- 視頻-----todo
- 聊天----todo
- 長列表swiper左右切換
- 第三方插件
- uview
- mescroll
- uCharts (圖表)
- 無名 (更新插件)
- 第三方模版
- 自定義基座
- 打包發行
- 要封裝的方法
- 緩存 cache.js
- 請求接口 request.js
- 工具類 util.js
- 小程序登錄 xcxLogin.js
- 版本更新 update.js
- 優質插件
- 更新插件----todo
- 語音
- 語音識別 (含上傳)
- 百度語音合成播報接口
- 官方常用組建
- input 輸入框
- image 圖片
- audio 音頻
- picker 選擇器
- video 視頻
- scroll-view 滾動視圖