The vue project realizes uploading pictures on Tencent Cloud

Preparations

By npm i cos-js-sdk-v5downloading cos-js-sdk-v5, and then in the component for uploading pictures, by import COS from 'cos-js-sdk-v5'introducing cos-js-sdk-v5.

front end

Analysis: here is the component of uploading files in element ui, action means the upload address; http-request means to override the default upload behavior, you can customize the upload implementation, the type is function, so the uploadimg here is a function; limit Indicates the maximum number of pictures that can be uploaded; list-type indicates the type of the file list; on-change indicates that the function will be triggered when the file is uploaded successfully or failed; auto-upload indicates whether to upload the file immediately after selecting the file; dialogVisible indicates when the image After the upload is successful, the picture will be displayed on the page; imgURL indicates the url address of the picture after the upload is successful;

<div style="display:flex;">
              <span style="display: inline-block;width: 91px;">身份证头像:</span>
              <el-upload
                action="#"
                :http-request="uploadImg"
                :limit='1'
                list-type="picture-card"
                :on-change="changeHandle"
                :auto-upload="true"
              >
                <i class="el-icon-plus" />
              </el-upload>
              <el-dialog :visible.sync="dialogVisible" append-to-body>
                <img width="100%" :src="imgURL" alt="">
              </el-dialog>
            </div>

js part

In the page that implements image upload, introduceimport COS from 'cos-js-sdk-v5'

import COS from 'cos-js-sdk-v5
data(){
	return{
	// 修改作品信息弹窗显示与否
      dialogVisible: false,
      // 图片文件
      imgFile: '',
      // 图片文件名称
      fileName: '',
      // 上传成功后的地址
      imgURL: '',
	}
},
methods:{
	// 获取该文件的对象,如下图1-1
    changeHandle (file, fileList) {
      this.imgFile = file
      this.fileName = file.name
    },
  // 实现上传图片
  uploadImg () {
      // 创建COS实例  获取签名
      //这里写你们后端提供的请求接口即可,这里调用的接口是为了获取tmpSecretId、tmpSecretKey、sessionToken、expiredTime这些值
      this.axios.post('接口地址').then(res=>{
        const data = res.data.content
        const cos = new COS({
        // 必选参数
        getAuthorization: (options, callback) => {
          const obj = {
            TmpSecretId: data.tmpSecretId,
            TmpSecretKey: data.tmpSecretKey,
            XCosSecurityToken: data.sessionToken,
            StartTime: data.startTime, // 时间戳,单位秒,如:1580000000
            ExpiredTime: data.expiredTime // 时间戳,单位秒,如:1580000900
          }
          callback(obj)
          // 上传图片  其中Bucket 和 Region找你们负责人拿,Bucke和Region是由公司负责人使用公司账户,登入腾讯云,获取回来的。
          //key 在前面加上路径写法可以生成文件夹
          cos.putObject({
            Bucket: 'xunahua-face-1307070664', /* 必须 */
            Region: 'ap-beijing', /* 存储桶所在地域,必须字段 */
            Key: '/img/' + new Date().getTime() + this.fileName, /* 必须 */
            StorageClass: 'STANDARD',
            Body: this.imgFile.name, // 上传文件对象
            onProgress: progressData => {
            }
          }, (err, data) => {
            // 将上传后的封面进行路径拼接保存到本地
            // console.log(err || data)
            const url = 'https://' + data.Location
            this.imgURL = url
          })
        }
      })
      })
    },

}

insert image description here
Figure 1-1 Object information after selecting a picture

Guess you like

Origin blog.csdn.net/i96249264_bo/article/details/119966429