当前位置:   article > 正文

uni-file-picker 上传图片至服务器

uni-file-picker
  1. <template>
  2. <view class="box">
  3. <view class="custom-title-box">
  4. <view class="status-bar-height-box" :style="{height: statusBarHeight + 'px'}"></view>
  5. <view class="title-box" :style="{height: titleHeight + 'px'}">
  6. <uni-icons type="back" class="left" size="30" color="#fff" @click="back()"></uni-icons>
  7. 发布
  8. </view>
  9. </view>
  10. <view class="empty-box" :style="{height: statusBarHeight + titleHeight + 'px'}" style="width: 100%;"></view>
  11. <view class="wrap-two">
  12. <textarea class="input" @input="bindTextAreaBlur" placeholder="记录美好瞬间~" :value="content" maxlength="1000" />
  13. <view class="text">{{length}}/1000字</view>
  14. </view>
  15. <view class="up">
  16. <uni-file-picker limit="9" @select="select" @success="success" @progress="progress" :auto-upload="true"
  17. fileMediatype="image" title="最多选择9张图片" mode="grid" v-model="imageValue"></uni-file-picker>
  18. </view>
  19. <view class="titlelist">
  20. <view class="title">
  21. <image src="../../static/img/select.png" mode=""></image>
  22. <view class="text">话题 <text class="span">添加一个话题吧,让更多人看到~(最多添加五个)</text></view>
  23. </view>
  24. <view class="btn" @click="isShow=1">
  25. + 添加
  26. </view>
  27. </view>
  28. <view class="tags">
  29. <view class="item" v-for="(item,index) in taglist" :key="index">
  30. #{{item.name||"无"}} <uni-icons type="closeempty" color="#fff" size="12"
  31. @click="taglist.splice(index,1)"></uni-icons>
  32. </view>
  33. </view>
  34. <view class="button-wrap">
  35. <view class="canel" @click="back">
  36. 取消
  37. </view>
  38. <view class="go" @click="uploadImages()">
  39. 发布
  40. </view>
  41. </view>
  42. <view class="mask" v-show="isShow" @click="isShow=0">
  43. <view class="pp" @tap.stop>
  44. <uni-icons type="closeempty" class="close" @click="isShow=0"></uni-icons>
  45. <view class="title1">
  46. 添加话题
  47. </view>
  48. <input type="text" :value="tag" @input="gettag" placeholder="最多不超过5个字" maxlength="5" />
  49. <view class="button" @click="add">
  50. 添加
  51. </view>
  52. </view>
  53. </view>
  54. </view>
  55. </template>
  56. <script>
  57. import {
  58. myRequest,
  59. baseURL
  60. } from '../../api';
  61. export default {
  62. data() {
  63. return {
  64. statusBarHeight: 0, // 状态栏高度
  65. titleHeight: 0, // 标题栏高度
  66. content: "",
  67. length: 0,
  68. imageValue: [
  69. {
  70. name:"",
  71. path:"http://tmp/HoMUSHGao2Xdf9c107669734cd1cb61af4393e7c4ef0.jpg",
  72. url:"http://tmp/HoMUSHGao2Xdf9c107669734cd1cb61af4393e7c4ef0.jpg"
  73. }
  74. ],
  75. isShow: false,
  76. tag: "",
  77. taglist: [{
  78. name: '宠物猫'
  79. },
  80. {
  81. name: "心情不错"
  82. }
  83. ],
  84. imagelist:[]
  85. }
  86. },
  87. onLoad() {
  88. },
  89. onReady() {
  90. uni.getSystemInfo({
  91. success: res => {
  92. this.statusBarHeight = res.statusBarHeight
  93. }
  94. })
  95. const btnInfo = uni.getMenuButtonBoundingClientRect();
  96. this.titleHeight = (btnInfo.top - this.statusBarHeight) * 2 + btnInfo.height
  97. },
  98. methods: {
  99. back() {
  100. uni.navigateBack()
  101. },
  102. gettag(e) {
  103. this.tag = e.detail.value
  104. },
  105. bindTextAreaBlur(e) {
  106. this.content = e.detail.value
  107. this.length = e.detail.value.length
  108. console.log(e.detail.value.length)
  109. },
  110. add() {
  111. this.taglist.push({
  112. name: this.tag
  113. })
  114. this.tag = ""
  115. this.isShow = 0
  116. },
  117. select(e) {
  118. this.imageValue.push({
  119. path: e.tempFilePaths[0],
  120. url: e.tempFilePaths[0],
  121. name: ''
  122. })
  123. console.log('选择文件:', e)
  124. },
  125. progress(e) {
  126. console(e, "上传中")
  127. },
  128. success(e) {
  129. console.log('上传成功')
  130. },
  131. // up() {
  132. // const tempFilePaths = e.tempFilePaths[0];
  133. // },
  134. async uploadImages() {
  135. for (const image of this.imageValue) {
  136. await this.uploadImage(image.path);
  137. }
  138. },
  139. async uploadImage(imagePath) {
  140. // 将图片路径转化为 uni-app 的临时文件路径
  141. const tempFilePath = await uni.getImageInfo({
  142. src: imagePath
  143. }).then(info => info.path);
  144. // 上传临时文件到服务器
  145. console.log(`Uploading image: path=${imagePath}`);
  146. const response = await uni.uploadFile({
  147. url: baseURL + "?s=/ApiImageupload/uploadImg", //仅为示例,非真实的接口地址
  148. filePath: tempFilePath,
  149. name: 'file',
  150. success: (res) => {
  151. const img=JSON.parse(res.data).url
  152. this.imagelist.push({
  153. url:img
  154. })
  155. console.log(JSON.parse(res.data).url)
  156. // this.isShow=false
  157. // console.log(this.headimg)
  158. // console.log(JSON.parse(res.data.info.url),"666");
  159. }
  160. });
  161. console.log(`Image uploaded: path=${imagePath}, response=${response}`);
  162. }
  163. },
  164. }
  165. </script>
  166. <style lang="scss">
  167. .custom-title-box {
  168. width: 750rpx;
  169. position: fixed;
  170. top: 0;
  171. left: 0;
  172. z-index: 999;
  173. background-color: #9263FE;
  174. .title-box {
  175. // background-color: red;
  176. display: flex;
  177. justify-content: center;
  178. align-items: center;
  179. font-size: 36rpx;
  180. color: #FFFFFF;
  181. >image {
  182. width: 346rpx;
  183. height: 96rpx;
  184. }
  185. .left {
  186. width: 60rpx;
  187. height: 60rpx;
  188. position: absolute;
  189. left: 24rpx;
  190. bottom: 10rpx;
  191. }
  192. }
  193. }
  194. .box {
  195. width: 750rpx;
  196. // height: 1624rpx;
  197. background-color: #F8F8F8;
  198. position: relative;
  199. }
  200. .wrap-two {
  201. width: 750rpx;
  202. background: #FFFFFF;
  203. // border-radius: 0rpx 0rpx 0rpx 0rpx;
  204. // opacity: 0.8;
  205. .input {
  206. margin: 0 auto;
  207. // margin-top: 24rpx;
  208. width: 710rpx;
  209. height: 252rpx;
  210. border-radius: 6rpx 6rpx 6rpx 6rpx;
  211. opacity: 1;
  212. // border: 1rpx solid rgba(0,0,0,0.15);
  213. font-size: 28rpx;
  214. font-family: PingFang SC-Medium, PingFang SC;
  215. font-weight: 400;
  216. color: #3d3d3d;
  217. padding: 20rpx;
  218. overflow: auto;
  219. // padding-top: 0rpx;
  220. }
  221. .text {
  222. width: 726rpx;
  223. text-align: right;
  224. font-size: 24rpx;
  225. font-family: PingFang SC-Medium, PingFang SC;
  226. font-weight: 400;
  227. color: rgba(61, 61, 61, 0.25);
  228. padding-right: 24rpx;
  229. }
  230. }
  231. .up {
  232. width: 702rpx;
  233. padding: 24rpx;
  234. margin: 0 auto;
  235. background-color: #FFFFFF;
  236. }
  237. .titlelist {
  238. width: 750rpx;
  239. height: 50rpx;
  240. background-color: #FFFFFF;
  241. position: relative;
  242. .title {
  243. position: relative;
  244. display: flex;
  245. >image {
  246. position: absolute;
  247. top: 4rpx;
  248. left: 40rpx;
  249. width: 45rpx;
  250. height: 32rpx;
  251. opacity: 0.6;
  252. }
  253. .text {
  254. position: absolute;
  255. left: 60rpx;
  256. font-size: 28rpx;
  257. font-family: PingFang SC-Medium, PingFang SC;
  258. font-weight: 400;
  259. color: #333333;
  260. }
  261. .span {
  262. margin-left: 20rpx;
  263. font-size: 20rpx;
  264. font-family: PingFang SC-Medium, PingFang SC;
  265. font-weight: 400;
  266. color: rgba(61, 61, 61, 0.6);
  267. }
  268. }
  269. .btn {
  270. width: 84rpx;
  271. height: 34rpx;
  272. border-radius: 6rpx 6rpx 6rpx 6rpx;
  273. opacity: 1;
  274. border: 1rpx solid rgba(51, 51, 51, 0.6);
  275. font-size: 18rpx;
  276. font-family: PingFang SC-Regular, PingFang SC;
  277. font-weight: 400;
  278. color: rgba(51, 51, 51, 0.6);
  279. text-align: center;
  280. line-height: 34rpx;
  281. position: absolute;
  282. right: 20rpx;
  283. }
  284. }
  285. .tags {
  286. width: 750rpx;
  287. height: 80rpx;
  288. display: flex;
  289. background-color: #FFFFFF;
  290. align-items: center;
  291. border-bottom: 2rpx solid rgba(216, 216, 216, 0.25);
  292. .item {
  293. // width: 100rpx;
  294. height: 40rpx;
  295. background: rgba(188, 112, 254, 0.9);
  296. border-radius: 6rpx 6rpx 6rpx 6rpx;
  297. opacity: 1;
  298. margin-left: 24rpx;
  299. font-size: 18rpx;
  300. font-family: PingFang SC-Regular, PingFang SC;
  301. font-weight: 400;
  302. color: #FFFFFF;
  303. text-align: center;
  304. line-height: 40rpx;
  305. padding: 5rpx;
  306. }
  307. }
  308. .mask {
  309. width: 100%;
  310. height: 100vh;
  311. position: fixed;
  312. top: 0;
  313. bottom: 0;
  314. background-color: rgba(0, 0, 0, 0.6);
  315. z-index: 998;
  316. .pp {
  317. overflow: hidden;
  318. position: absolute;
  319. top: 50%;
  320. left: 50%;
  321. transform: translate(-50%, -50%);
  322. width: 702rpx;
  323. height: 400rpx;
  324. background: #FFFFFF;
  325. border-radius: 12rpx 12rpx 12rpx 12rpx;
  326. opacity: 1;
  327. display: flex;
  328. align-items: center;
  329. justify-content: space-around;
  330. .title1 {
  331. position: absolute;
  332. top: 40rpx;
  333. }
  334. >input {
  335. position: absolute;
  336. top: 128rpx;
  337. width: 586rpx;
  338. height: 80rpx;
  339. background: rgba(216, 216, 216, 0.5);
  340. border-radius: 10rpx 10rpx 10rpx 10rpx;
  341. opacity: 1;
  342. font-size: 28rpx;
  343. font-family: PingFang SC-Medium, PingFang SC;
  344. font-weight: 400;
  345. color: rgba(61, 61, 61, 0.3);
  346. padding-left: 20rpx;
  347. }
  348. .button {
  349. position: absolute;
  350. top: 258rpx;
  351. width: 606rpx;
  352. height: 88rpx;
  353. background: #9263FE;
  354. border-radius: 10rpx 10rpx 10rpx 10rpx;
  355. opacity: 1;
  356. font-size: 32rpx;
  357. font-family: PingFang SC-Medium, PingFang SC;
  358. font-weight: 400;
  359. color: #FFFFFF;
  360. text-align: center;
  361. line-height: 88rpx;
  362. }
  363. .close {
  364. position: absolute;
  365. right: 10rpx;
  366. top: 10rpx;
  367. }
  368. }
  369. }
  370. .button-wrap {
  371. position: fixed;
  372. bottom: 100rpx;
  373. width: 750rpx;
  374. height: 100rpx;
  375. background: #FFFFFF;
  376. border-radius: 0rpx 0rpx 0rpx 0rpx;
  377. display: flex;
  378. justify-content: space-around;
  379. align-items: center;
  380. .canel {
  381. width: 312rpx;
  382. height: 88rpx;
  383. border-radius: 6rpx 6rpx 6rpx 6rpx;
  384. opacity: 1;
  385. border: 2rpx solid #9263FE;
  386. font-size: 32rpx;
  387. font-family: PingFang SC-Bold, PingFang SC;
  388. font-weight: 400;
  389. color: #9263FE;
  390. text-align: center;
  391. line-height: 88rpx;
  392. }
  393. .go {
  394. width: 316rpx;
  395. height: 92rpx;
  396. background: #9263FE;
  397. border-radius: 6rpx 6rpx 6rpx 6rpx;
  398. opacity: 1;
  399. font-size: 32rpx;
  400. font-family: PingFang SC-Bold, PingFang SC;
  401. font-weight: 400;
  402. color: #FFFFFF;
  403. text-align: center;
  404. line-height: 92rpx;
  405. }
  406. }
  407. </style>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/183474
推荐阅读
相关标签
  

闽ICP备14008679号