vue上传图片到腾讯云和上传视频到云点播

本文展示了如何使用Vue.js实现图片上传功能,并详细介绍了将视频上传至腾讯云点播的组件代码,涵盖了从前端到云端的完整流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

上传图片组件代码(上传视频到云点播代码在下面)

<!--
  此文件是上传图片的公共组件
  父组件传过来的参数:
  一:promptText,图片上面的限制文字
  二:mostNum,最多上传几张
  三:imgSize,图片的建议尺寸
  四:imgUrlArr,需要提前展示的图片的链接数组
  要传给父组件的参数:
  一:getImgUrlId,每一张上传成功之后将图片id传给父组件
  二:delImg,删除图片的时候,将删除的这张图片的索引传给父组件
-->
<template>
  <div id="UploadImg" class="upload-file-box dialog-margintop" :class="{
    
    'pb20': masterGraph}">
    <div class="upload-img">
      <div style="font-size:12px;color:#9BAAB5">{
  
  {promptText}}</div>
      <el-button v-if="isShowAdd">
        <div class="add-img">
          <i class="el-icon el-icon-plus"></i>
        </div>
        <form action="" id="uploadForm">
          <input
            type="file"
            title=""
            name="uploads"
            id="uploads"
            accept="image/png, image/jpeg, image/jpg"
            multiple
            ref="dowloadImg"
            @change="uploadImg($event, 1)" />
        </form>
      </el-button>
      <el-button v-for="(item, index) in imgBaseUrlArr" :key="index">
        <img :src="item">
        <i v-if="masterGraph && isMasterGraph == imgIdArr[index]" class="master-btn" @click="setMasterGraph(index)">已是主图</i>
        <i v-else-if="masterGraph" class="master-btn" @click="setMasterGraph(index)">设为主图</i>
        <div class="mark-box">
          <i @click="delImgMsg(index)" class="el-icon el-icon-delete delete_img"></i>
          <i @click="seeImgMsg(index)" class="el-icon el-icon-zoom-in big_img"></i>
        </div>
      </el-button>
    </div>
    <!-- 剪切图片的弹窗 -->
    <el-dialog class="margin-top" title="图片剪裁" :visible.sync="dialogVisible" append-to-body :close-on-click-modal="false">
      <div class="cropper-content">
        <div class="cropper" style="text-align:center">
          <vue-cropper
            ref="cropper"
            :img="option.img"
            :outputSize="option.size"
            :outputType="option.outputType"
            :info="true"
            :full="option.full"
            :canMove="option.canMove"
            :canMoveBox="option.canMoveBox"
            :original="option.original"
            :fixedNumber="option.fixedNumber"
            :autoCrop="option.autoCrop"
            :fixed="option.fixed"
            :centerBox="option.centerBox"
            :infoTrue="option.infoTrue"
            :fixedBox="option.fixedBox"
          ></vue-cropper>
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button  type="primary" class="info-btn" @click="dialogVisible = false">取 消</el-button>
        <el-button :disabled="btnDisabled" class="promary-btn" type="primary" @click="finish">{
  
  {dialogText}}</el-button>
      </div>
      <div class="preview-box">
        <section class="pre-item">
          <!-- <p>截图框大小</p> -->
          <div class="show-preview" :style="{
      
      'width': previews.w + 'px', 'height': previews.h + 'px',  'overflow': 'hidden', 'margin': '5px'}">
       
### SpringBoot与Vue结合腾讯云实现视频上传点播功能的实现方案 #### 后端部分(SpringBoot) 后端主要负责处理业务逻辑以及与腾讯云SDK的对接。以下是具体实现方法: 1. **引入依赖** 需要在`pom.xml`中添加腾讯云SDK的相关依赖,用于调用腾讯云API完成视频上传等功能[^2]。 ```xml <!-- 腾讯视频点播VOD --> <dependency> <groupId>com.tencentcloudapi</groupId> <artifactId>tencentcloud-sdk-java</artifactId> <version>3.1.2</version> </dependency> ``` 2. **配置文件设置** 在`application.yml`或`application.properties`中配置腾讯云相关参数,包括`secretId`、`secretKey`等必要信息[^2]。 ```yaml vod: secretId: your-secret-id secretKey: your-secret-key vodSubAppId: your-vod-sub-app-id procedure: your-procedure-name playKey: your-play-key licenseUrl: your-license-url ``` 3. **创建服务类** 编写一个专门的服务类来封装与腾讯云交互的功能,例如上传视频到云端并获取播放链接。 ```java @Service public class TencentCloudVodService { private final String secretId; private final String secretKey; public TencentCloudVodService(@Value("${vod.secretId}") String secretId, @Value("${vod.secretKey}") String secretKey) { this.secretId = secretId; this.secretKey = secretKey; } /** * 上传本地视频腾讯云 */ public String uploadVideo(String filePath) throws Exception { Credential cred = new Credential(secretId, secretKey); ClientProfile clientProfile = ClientProfile.getDefault(); HttpProfile httpProfile = new HttpProfile(); httpProfile.setEndpoint("vod.tencentcloudapi.com"); clientProfile.setHttpProfile(httpProfile); VodClient client = new VodClient(cred, "", clientProfile); ApplyUploadRequest req = new ApplyUploadRequest(); req.setMediaType("mp4"); // 媒体类型 req.setMediaName("test-video.mp4"); // 文件名 req.setCoverType(""); // 封面类型 ApplyUploadResponse response = client.ApplyUpload(req); UploadInfo uploadInfo = response.getUploadInfo(); // 使用返回的信息执行实际上传操作... return "https://example.com/" + uploadInfo.getFileId(); // 返回播放URL } } ``` 4. **控制器接口定义** 创建RESTful API供前端调用,允许用户提交视频文件并触发上传流程。 ```java @RestController @RequestMapping("/api/vod") public class VideoController { private final TencentCloudVodService tencentCloudVodService; public VideoController(TencentCloudVodService tencentCloudVodService) { this.tencentCloudVodService = tencentCloudVodService; } @PostMapping("/upload") public ResponseEntity<String> uploadVideo(@RequestParam("file") MultipartFile file) { try { File tempFile = convertMultiPartToFile(file); // 处理Multipart文件转换 String videoUrl = tencentCloudVodService.uploadVideo(tempFile.getAbsolutePath()); return ResponseEntity.ok(videoUrl); } catch (Exception e) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage()); } } private File convertMultiPartToFile(MultipartFile multipartFile) throws IOException { File convFile = new File(multipartFile.getOriginalFilename()); FileOutputStream fos = new FileOutputStream(convFile); fos.write(multipartFile.getBytes()); fos.close(); return convFile; } } ``` --- #### 前端部分(Vue) 前端主要用于构建用户界面并与后端通信,提供友好的用户体验。 1. **安装必要的库** Vue项目需借助Axios或其他HTTP客户端发送请求给后端API。 ```bash npm install axios vue-upload-component ``` 2. **组件设计** 设计一个简单的表单页面让用户选择要上传视频文件,并显示成功后的播放链接。 ```vue <template> <div> <h3>视频上传</h3> <input type="file" id="video-file" /> <button @click="handleUpload">上传</button> <div v-if="uploaded"> <h4>已上传:</h4> <iframe :src="playbackUrl" width="640" height="360"></iframe> </div> </div> </template> <script> import axios from 'axios'; export default { data() { return { uploaded: false, playbackUrl: '' }; }, methods: { async handleUpload() { const fileInput = document.getElementById('video-file'); const formData = new FormData(); formData.append('file', fileInput.files[0]); try { const res = await axios.post('/api/vod/upload', formData, { headers: { 'Content-Type': 'multipart/form-data' } }); this.playbackUrl = res.data; this.uploaded = true; } catch (err) { console.error(err.message); } } } }; </script> ``` --- #### 整合说明 前后端通过标准JSON格式的数据交换协议进行协作。当用户点击“上传”按钮时,前端会将选中的视频文件打包并通过POST请求传递给后端;而后端接收到文件后将其保存临时路径再交由腾讯云SDK完成最终存储及转码工作,最后反馈播放地址回前端渲染展示。 此架构充分利用了SpringBoot框架的强大能力简化开发周期的同时也保障系统的稳定性[^3],而Vue作为现代化前端框架则极大提升了UI表现力与互动效果[^5]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值