微信小程序上传文件到java服务器,有完整代码,保姆级,直接复制可用

news/2025/2/16 6:59:13/

微信小程序项目需要用户选择文件上传文件到服务器,折腾了好大一会,参考了网上的代码,并略微修改,方便自己,也方便别人,记录下来

1.微信小程序代码

.wxml

  <view class="usermotto"><text class="user-motto" bindtap="saveFile">{{motto}}</text></view>

.js

saveFile() {wx.chooseMedia({count: 1, // 可选文件数量。因为是头像上传,只一个文件就可mediaType: ["image"], // 文件类型:这里只是图片,其他使用看官网success: res => {this.uploadFile(res.tempFiles[0]); // 返回的数据里有我们后面上传需要的文件,保存下来},fail: err => {console.log(err, "personal.js >>>> 个人资料 上传头像")}})},uploadFile(file) {//console.log(file.tempFilePath);wx.uploadFile({filePath: file.tempFilePath, // 上一步操作中带过来的文件name: 'file', // 接口中要求的formData类型数据的参数url: 'http://localhost:8080/handle-video/common/uploadFile', // 接口formData: { // 其他数据类型的参数code: '',type: ''},success: res => {console.log(res,'上传成功');},fail: err => {console.log(err, 'personal.js >>>> 个人资料')}})

2.java代码

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;@RestController
@RequestMapping("/handle-video/common")
public class CommonController {@RequestMapping("/uploadFile")public String uploaddFie(MultipartFile file){return file.getOriginalFilename();}
}

参考文档1:微信上传 wx.chooseMedia和wx.uploadFile使用,java springboot后端跑通_老饼干的博客-CSDN博客

参考文档2:https://www.jianshu.com/p/49227427d145


http://www.ppmy.cn/news/701035.html

相关文章

微信支付实战(完整的代码,复制即可用)

公众号介绍 https://developers.weixin.qq.com/doc/offiaccount/Getting_Started/Overview.html 最重要的一点&#xff1a; 公众平台以access_token为接口调用凭据&#xff0c;来调用接口&#xff0c;所有接口的调用需要先获取access_token&#xff0c;access_token在2小时内有…

微信camera拍照组件的使用(uniapp小程序)代码可直接复制看效果

微信camera官方文档&#xff1a;https://developers.weixin.qq.com/miniprogram/dev/component/camera.html html 整体效果 样式可以自行定义的一个拍照组件 未找到摄像头是因为台式机电脑没有摄像头 真机测试可以使用 <view class"tackpic"><view class&qu…

520微信代码轰炸

写一个脚本&#xff0c;在520那天发给你的小可爱。 # -*- coding : utf-8 -*- # Time : 2022/5/19 13:36 # Author : wkb import time,os import pyautogui,pyperclip time.sleep(5) for i in range(10):#pyautogui.click(662,748)pyperclip.copy("代码轰炸&#…

Python趣味代码(一):微信信息轰炸

1.安装模块 首先需要在电脑上安装好pyautogui、pyperclip两个模块 # Windos系统安装命令 pip install pyautogui pip install pyperclip# Mac系统安装命令 pip3 install pyautogui pip3 install pyperclip 2.功能实现 将整个流程分为三大块&#xff1a; 一、获取发信内容 二、…

uniapp微信小程序授权登录流程(代码直接复制可用)

1.写一个点击登录的按钮 <view tap"login">一键登录</view> 2.写点击事件 login() {wx.getUserInfo({success: (res) > {console.log(获取到openId, res);}})let that this;uni.getUserProfile({desc: 用于完善用户资料,lang: zh_CN,success: (res) …

爱心代码--C语言特供(可直接复制,亲测有效)

情人节到了&#xff0c;作为一名程序员&#xff0c;我们拥有属于我们的浪漫。 这里我总结了几种常见的爱心代码&#xff0c;简单易上手。 一.这是一种最为常见的爱心代码 #include<stdio.h> #include<Windows.h>int main() {float x, y, a;for (y 1.5; y > -1.…

微信小程序页面跳转方式+跳转小程序(直接复制代码可用)

一. 微信小程序跳转页面方法 1.跳转到 tabBar 页面 wx.switchTab({url: /index }) 2.跳转到其他页面&#xff08;非tabBar页&#xff09; //redirectTo方法&#xff08;会关闭当前页面&#xff09; wx.redirectTo({url: /page//页面路径 })//navigateTo方法&#xff08;不会…

微信8.0自动发送炸弹python脚本

import itchat import argparsedef get_arguments():parser argparse.ArgumentParser(description文献表情包发送)parser.add_argument(--name, typestr, default"xxx",help要发送的人的微信备注)parser.add_argument(--group, typestr, default"person",…