uni 您所在的位置:网站首页 php实现文件上传功能效果图是什么 uni

uni

2024-02-04 14:09| 来源: 网络整理| 查看: 265

配置Tomcat 1.1、添加upload文件夹

在webapps\Root文件夹下创建用于接收上传文件的upload文件夹

1.2、修改conf\web.xml设置允许上传文件 readonly false 1.3、修改conf\server.xml设置端口号,避免跟springboot冲突 二、uni-app代码: //_self为在export default外面作用域外定义的全局变量,用来等价代换 _self = this; //第一步:打开手机相册或者文件管理器选择文件 uni.chooseImage({ count:1,//允许上传一张图片 sizeType:['original','compressed'],//可以指定是原图还是压缩图,默认二者都有 sourceType:['album'],//从相册选择 success:function(res){ //获得选择好的文件,就算只有一个文件也是数组 const tempFilePaths = res.tempFilePaths; //第二步:把选择的文件上传到服务器 const uploadTask = uni.uploadFile({ url:'http://localhost:8070/upload/file',//后端接口url filePath:tempFilePaths[0], name:'fileName',//和后端接口需要的参数名一致 success: (res) => { console.log("图片路径为:"+res.data) _self.formData.logoPath=res.data//获取到图片路径 } }) //获取文件的上传进度 uploadTask.onProgressUpdate(function(res){ console.log('上传进度:'+res.progress) console.log('已经上传的数据长度:'+res.totalBytesSent) console.log('预计需要上传的数据总长度:'+res.totalBytesExpectedToSend) }) } })

如果是文件:

上传文件方法也可以上传图片,只需要把extension中改为图片对应为后缀名即可

_self = this; uni.chooseFile({ //第一步:选择文件 count: 1, //默认100 extension:['.pdf','.doc','.xlsx','.docx','.xls'], success: function (res) { const tempFilePaths = res.tempFilePaths;//若果只选择一个文件,这个文件就是数组的第一个元素 //第二步:把选择的文件上传到服务器 uni.uploadFile({ url:'http://localhost:8070/upload/file', filePath:tempFilePaths[0], name:'fileName', success: (res) => { console.log(res.data) _self.formData.attachmentpath = res.data } }) } }) } 三、spring boot代码: JesyFileUpload工具类: /** * 上传文件 * * @param file --文件名 * @param serverUrl --服务器路径http://127.0.0.1:8080/ssm_image_server * @return * @throws IOException */ public static String uploadFile(MultipartFile file, String serverUrl) throws IOException { //重新设置文件名 String newFileName = new Date().getTime()+""; //将当前时间获得的毫秒数拼接到新的文件名上 //随机生成一个3位的随机数 Random r = new Random(); for(int i=0; i


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有