欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > 能源 > uniapp springboot 上传demo

uniapp springboot 上传demo

2025/2/24 6:42:22 来源:https://blog.csdn.net/alisande1/article/details/144553707  浏览:    关键词:uniapp springboot 上传demo

前端

<template><view class="container"><button type="primary" @click="chooseVideo">选择视频</button><video v-if="localUrl" :src="localUrl" controls></video><view style="margin-bottom: 40rpx;"></view><button type="warn" @click="uploadVideo" :disabled="!localUrl">上传视频</button></view>
</template><script>
export default {data() {return {localUrl: ''};},methods: {chooseVideo() {uni.chooseVideo({success: (res) => {this.localUrl = res.tempFilePath;}});},uploadVideo() {if (!this.localUrl) {uni.showToast({title: '请先选择视频',icon: 'none'});return;}uni.uploadFile({url: 'http://127.0.0.1:9035/web/sms/test02', // 替换为你的服务器地址filePath: this.localUrl,name: 'file',formData: {'user': 'test'},success: (uploadRes) => {console.log('上传成功', uploadRes.data);uni.showToast({title: '上传成功'});},fail: (err) => {console.error('上传失败', err);uni.showToast({title: '上传失败',icon: 'none'});}});}}
};
</script><style scoped>
/* Add your styles here */
</style>

后端

private static final String UPLOAD_DIR = "d:/logs/uploads/";@SneakyThrows
@PostMapping("/test02")
public ResponseEntity<String> test02(@RequestParam("file") MultipartFile file) {if (file.isEmpty()) {return ResponseEntity.badRequest().body("Please select a file to upload.");}// Create the uploads directory if it doesn't existFile uploadDir = new File(UPLOAD_DIR);if (!uploadDir.exists()) {uploadDir.mkdirs();}// Save the file on the serverbyte[] bytes = file.getBytes();Path path = Paths.get(UPLOAD_DIR + file.getOriginalFilename());Files.write(path, bytes);return ResponseEntity.ok("You successfully uploaded " + file.getOriginalFilename() + "!");}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词