经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » 微信小程序 » 查看文章
微信小程序学习笔记之文件上传、下载操作图文详解
来源:jb51  时间:2019/3/29 11:45:12  对本文有异议

本文实例讲述了微信小程序学习笔记之文件上传、下载操作。分享给大家供大家参考,具体如下:

前面介绍了微信小程序登录API与获取用户信息操作。这里再来介绍一下文件的上传与下载操作。

【文件上传】wx.uploadFile

(以上传图片为例)

后台上传接口Upload.php:(tp5)

  1. <?php
  2. namespace app\home\controller;
  3. use think\Controller;
  4. class Upload extends First
  5. {
  6. //上传图片API
  7. public function upImg() {
  8. $arr = array('state'=>0,'msg'=>'上传失败','filepath'=>'');
  9. $file = request()->file('file');
  10. if($file){
  11. $info = $file->move('upload/weixin/');
  12. if ($info) {
  13. $arr['state'] = 1;
  14. $arr['msg'] = '上传成功';
  15. $arr['filepath'] = $info->getSaveName();
  16. }
  17. }
  18. return json($arr);
  19. }
  20. }

前台页面upload.wxml:

  1. <image src='{{imgpath}}' style='width:600rpx; height:600rpx' />
  2. <view>
  3. <button bindtap="upImg">点击选择上传图</button>
  4. </view>

前台upload.js:

  1. Page({
  2. data: {
  3. imgpath: ''
  4. },
  5. upImg: function (e) {
  6. var that = this
  7. wx.chooseImage({
  8. count: 1, // 默认最多一次上传9张图片
  9. sizeType: ['original', 'compressed'], // 允许原图和压缩图
  10. sourceType: ['album', 'camera'], // 允许相册和相机
  11. success(res) {
  12. const tempFilePaths = res.tempFilePaths
  13. wx.showToast({
  14. title: '正在上传...',
  15. icon: 'loading',
  16. mask: true,
  17. duration: 500
  18. })
  19. wx.uploadFile({
  20. url: 'https://www.msllws.top/Upload/upImg', //服务器上传接口
  21. filePath: tempFilePaths[0], //文件资源路径
  22. name: 'file',
  23. header: {
  24. 'Content-Type': 'Application/json'
  25. },
  26. success(res) {
  27. console.log(res)
  28. if (res.statusCode == 200){
  29. that.setData({
  30. imgpath: tempFilePaths
  31. })
  32. }
  33. }
  34. })
  35. }
  36. })
  37. }
  38. })

演示效果:

(其实是有正在上传...效果的,手机录屏没给录上。。)

 
查看服务器里面多了一张图片:

嗯哼~

 【文件下载】wx.downloadFile

(以下载一张图片为例)

在服务器目录下放一张图片1.jpg:

download.wxml:

  1. <image src='{{imgpath}}' style='width:600rpx; height:600rpx' />
  2. <view>
  3. <button bindtap="download">点击下载</button>
  4. </view>

download.js:

  1. Page({
  2. data: {
  3. imgpath: ''
  4. },
  5. download: function (e) {
  6. var that = this
  7. wx.showToast({
  8. title: '正在下载...',
  9. icon: 'loading',
  10. mask: true,
  11. duration: 500
  12. })
  13. wx.downloadFile({
  14. url: 'https://www.msllws.top/upload/1.jpg', //下载地址
  15. type: 'image', //下载的资源类型(imnage/audio/video)
  16. success: function (res) {
  17. console.log(res)
  18. if (res.statusCode == 200) {
  19. var filepath = res.tempFilePath
  20. that.setData({
  21. imgpath: filepath
  22. })
  23. }
  24. }
  25. })
  26. }
  27. })

演示效果:

 

希望本文所述对大家微信小程序开发有所帮助。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号