upload.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <template>
  2. <!-- 上传附件 -->
  3. <lsj-upload
  4. ref="lsjUpload"
  5. childId="upload1"
  6. :width="width"
  7. :height="height"
  8. :option="option"
  9. :size="size"
  10. :formats="formats"
  11. :debug="debug"
  12. :instantly="instantly"
  13. @progress=""
  14. @uploadEnd="onuploadEnd" >
  15. <view class="btn" :style="{width: width,height: height}">选择附件</view>
  16. </lsj-upload>
  17. </template>
  18. <script>
  19. import config from '@/config'
  20. const baseUrl = config.baseUrl
  21. import { getToken } from '@/utils/auth'
  22. export default{
  23. data(){
  24. return{
  25. //附件
  26. option: {
  27. // 上传服务器地址,需要替换为你的接口地址
  28. url: baseUrl+'/common/upload', // 该地址非真实路径,需替换为你项目自己的接口地址
  29. // 上传附件的key
  30. name: 'file',
  31. // 根据你接口需求自定义请求头,默认不要写content-type,让浏览器自适配
  32. header: {
  33. // 示例参数可删除
  34. 'Authorization': 'Bearer ' + getToken(),
  35. },
  36. // 根据你接口需求自定义body参数
  37. formData: {
  38. }
  39. },
  40. // 选择文件后是否立即自动上传,true=选择后立即上传
  41. instantly: true,
  42. // 必传宽高且宽高应与slot宽高保持一致
  43. width: '',
  44. height: '48rpx',
  45. // 限制允许上传的格式,空串=不限制,默认为空
  46. formats: 'doc,docx,xls,ppt,txt,pdf,zip,rar,word',
  47. // 文件上传大小限制
  48. size: 100,
  49. // 文件数量限制 默认10
  50. count: 5,
  51. // 文件回显列表
  52. files: new Map(),
  53. // 微信小程序Map对象for循环不显示,所以转成普通数组,不要问为什么,我也不知道
  54. wxFiles: [],
  55. // 是否打印日志
  56. debug: false,
  57. // 演示用
  58. tabIndex: 0,
  59. // filelist:[{originalFilename:'ceisi.docx',fileName:'/profile/upload/2023/03/05/ceisi_20230305125835A007.docx'}],
  60. filelist:[{fjName:'ceisi.docx',path:'/profile/upload/2023/03/05/ceisi_20230305125835A007.docx'}],
  61. }
  62. },
  63. methods:{
  64. onuploadEnd(item) {
  65. var newobj={}
  66. var responseText=JSON.parse(item.responseText)
  67. newobj.fjName=responseText.originalFilename;
  68. newobj.path=responseText.fileName;
  69. this.filelist.push(newobj)
  70. },
  71. getDelFj(idx){
  72. this.filelist.splice(idx,1)
  73. },
  74. getDown(e){
  75. uni.showLoading({
  76. title: '加载中'
  77. });
  78. var url=baseUrl+e;
  79. uni.downloadFile({
  80. url: url,//文件的下载路径
  81. success(result) {
  82. uni.hideLoading()
  83. var filePath = result.tempFilePath;
  84. uni.openDocument({
  85. filePath: filePath,
  86. showMenu: true,
  87. success: function (res) {
  88. console.log('打开文档成功');
  89. }
  90. });
  91. },
  92. fail(res) {uni.hideLoading()}
  93. })
  94. },
  95. }
  96. }
  97. </script>
  98. <style>
  99. </style>