uni-app文件上传下载

uni-app文件上传下载

vue

<template>
	<view>
		<button @click="upload">上传</button>
		<button @click="download">下载</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {}
		},
		methods: {
			// 上传
			upload: function() {
				uni.chooseImage({
					success: (chooseImageRes) => {
						const tempFilePaths = chooseImageRes.tempFilePaths;
						uni.uploadFile({
							url: 'http://192.168.1.105/file/upload',
							filePath: tempFilePaths[0],
							name: 'file',
							formData: {
								'user': 'test'
							},
							success: (uploadFileRes) => {
								console.log(uploadFileRes.data);
								uni.showToast({
									title: "上传成功",
									icon: "ok"
								});
							}
						});
					},
					fail: function(chooseImageRes) {
						uni.showToast({
							title: "取消上传",
							icon: "none"
						});
					}
				});
			}, // END OF upload,

			// 下载
			download: function() {
				uni.downloadFile({
					url: 'http://192.168.1.105/file/DWoVn7uIJZ6b07855d929239d528bd59a8a049b58ba1.jpg',
					success: (res) => {
						if (res.statusCode === 200) {
							uni.saveImageToPhotosAlbum({
								filePath: res.tempFilePath,
								success: function() {
									uni.showToast({
										title: "保存成功",
										icon: "ok"
									});
								},
								fail: function() {
									uni.showToast({
										title: "取消保存",
										icon: "none"
									});
								}
							});
						}
					}
				});
			}, // END OF download,

		}
	}
</script>

<style>

</style>

java

package com.cloud.controller;

import com.cloud.config.CloudConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @Deacription 在此编写描述信息
 * @Author Administrator
 * @Date 2021/1/17 13:53
 * @Version 1.0
 **/
@RequestMapping("file")
@RestController
public class FileController {
    @Autowired
    private CloudConfigurer cloud;

    @PostMapping("upload")
    public String uploadFile(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return "上传失败,请选择文件!";
        }
        String fileName = file.getOriginalFilename();
        String filePath = this.cloud.getSavePath();

        File dir = new File(filePath);
        if (!dir.exists()) {
            dir.mkdir();
        }
        try {
            File file1 = new File(filePath, fileName);
            file.transferTo(file1);

            return (filePath + fileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "上传失败!";
    }
}


  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值