栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

uniapp上传图片

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

uniapp上传图片

IDEA 实体类
package com.xiaohui.cms.entity;

public class FileInfo {

	private String fileName;//文件名称
	private boolean isSecret;//是否是私密文件
	private String absolutPath;//文件的绝对路径
	private String relativePath;//文件的相对路径

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getAbsolutPath() {
		return absolutPath;
	}

	public void setAbsolutPath(String absolutPath) {
		this.absolutPath = absolutPath;
	}

	public String getRelativePath() {
		return relativePath;
	}

	public void setRelativePath(String relativePath) {
		this.relativePath = relativePath;
	}

	public boolean isSecret() {
		return isSecret;
	}

	public void setSecret(boolean isSecret) {
		this.isSecret = isSecret;
	}

	public FileInfo() {
	}

	public FileInfo(String fileName, boolean isSecret, String absolutPath, String relativePath) {
		super();
		this.fileName = fileName;
		this.isSecret = isSecret;
		this.absolutPath = absolutPath;
		this.relativePath = relativePath;
	}

	@Override
	public String toString() {
		return "FileInfo [fileName=" + fileName + ", isSecret=" + isSecret + ", absolutPath=" + absolutPath
				+ ", relativePath=" + relativePath + "]";
	}

}
Controller类
package com.xiaohui.cms.controller;

import com.xiaohui.cms.entity.FileInfo;
import com.xiaohui.cms.utils.JesyFileUploadUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/upload")
public class FileController {
    @PostMapping("/file")
    @ResponseBody
    @CrossOrigin(origins = "*")
    public String upload(MultipartFile file) {
        String path =null;
        try {
            FileInfo fileInfo = JesyFileUploadUtil.uploadFile(file ,"http://localhost:8060");//上传地址
            path =fileInfo.getRelativePath();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return path;
    }

    }

 HBuilder X 页面上传

添加页面

uni 界面


					选择标题图(图片分辨率建议为:200px*200px)
				
				

变量

    var _self ;//声明
	export default {
		data() {
			return {
				imgpPath: null,//图片变量
				loading: false,//进度变量
        }

	}
}

函数

			upload(){
				_self =this;
				_self.loading =true;
				//选择图片 使用uin.chooseImage
				uni.chooseImage({
					count:1,
					sizeType:['original','compressed'],//original:原图:compressed:压缩图 指定上传的图片是原图 还是压缩图
					sourceType:['album'],//从相册选择
					success: (res) => {
						const temFilePath = res.tempFilePaths;//图片选择完成后 获取该图片的路劲
						
						//上传图片 使用uni.upLoadFile
						uni.uploadFile({
							url:'http://127.0.0.1:8070/upload/file',//上传文件的Controller
							filePath: temFilePath[0],//要上传的文件路径
							name:'file',
							success: (res) => {
								console.log(res);
								_self.loading =false;//进度条
								_self.imgpPath =res.data;
							}
						});
					}
				})
			}
页面列表

					
				

data

data() {
			return {
				baserUrl:'http://localhost:8060',
				
			}
		},

 页面 完整代码






添加页面






转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1008970.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号