• 腾讯云cos上传文件


    需求 :将文件上传到 cos中

    先安装

    npm i cos-js-sdk-v5 --save

     上传的代码

    import React, { useState } from 'react';
    import {  Button, Form, Upload, Icon, message } from 'antd';
    import COS from 'cos-js-sdk-v5'; 
    
    
    const CreateFileDrawer = props => {
        const { fileManagementStore } = props;
        const [fileUrl, setFileUrl] = useState();
        const [fileName, setFileName] = useState();
    
        const uploadProps = {
                beforeUpload: file => {
                    let maxSize = 1024 * 1024 * 50;
    
                    let index = file.name?.lastIndexOf('.');
                    let type = file.name?.slice(index + 1);
    
                    if (type === 'doc' || type === 'docx') {
                        type = 'word';
                    }
                    let pattern = /(pdf)|(word)$/;
                    if (!pattern.test(type)) {
                        return message.error('文件格式不正确');
                    }
    
                    if (file.size > maxSize) {
                        return message.error('文件大小不能超过50M');
                    }
    
                    let cosStsData = fileManagementStore.cosStsData; //从后端获取查询COS临时秘钥信息
    
                    let cos = new COS({
                        // 必选参数
                        getAuthorization: function (options, callback) {
                            // 服务端 JS 和 PHP 例子:https://github.com/tencentyun/cos-js-sdk-v5/blob/master/server/
                            // 服务端其他语言参考 COS STS SDK :https://github.com/tencentyun/qcloud-cos-sts-sdk
                            // STS 详细文档指引看:https://cloud.tencent.com/document/product/436/14048
                            callback({
                                TmpSecretId: cosStsData.Credentials.TmpSecretId,
                                TmpSecretKey: cosStsData.Credentials.TmpSecretKey,
                                SecurityToken: cosStsData.Credentials.Token,
                                // 建议返回服务器时间作为签名的开始时间,避免用户浏览器本地时间偏差过大导致签名错误
                                StartTime: cosStsData.startTime, // 时间戳,单位秒,如:1580000000
                                ExpiredTime: cosStsData.ExpiredTime // 时间戳,单位秒,如:1580000900
                            });
                        }
                    });
    
                    cos.putObject(
                        {
                            Bucket: cosStsData.Bucket  /* 必须 */,
                            Region: cosStsData.Region /* 存储桶所在地域,必须字段 */,
                            Key: `userImage/${cosStsData.ExpiredTime}${file.uid}` /* 必须对象在存储桶中的唯一标识 */,
                            Body: file // 上传文件对象
                        },
                        function (err, data) {
                            console.log(err || data);
                            if (!err && data.statusCode == 200) {
                                setFileName(file.name);
                                setFileUrl(data.Location);
                            }
                        }
                    );
                    return false;
                }
            };
            return (
    
                    <Form labelCol={{ span: 24 }}>
    
                            <Form.Item label="文件">
                     
                                    <Upload {...uploadProps}>
                                        <Button>
                                            <Icon type="upload" /> 上传文件
                                        </Button>
                                        <span style={{ marginLeft: '20px' }}>{fileName}</span>
                                    </Upload>
                               
                                <p>支持pdf、word文件,文件大小不能超过50M</p>
                            </Form.Item>                   
                    </Form>
            );
    };

      

     
  • 相关阅读:
    在Ubuntu18.04.2LTS上安装搜狗输入法
    生活点滴:java基础知识细化
    乘风破浪:LeetCode真题_041_First Missing Positive
    乘风破浪:LeetCode真题_040_Combination Sum II
    乘风破浪:LeetCode真题_039_Combination Sum
    乘风破浪:LeetCode真题_038_Count and Say
    乘风破浪:LeetCode真题_037_Sudoku Solver
    乘风破浪:LeetCode真题_036_Valid Sudoku
    乘风破浪:LeetCode真题_035_Search Insert Position
    乘风破浪:LeetCode真题_034_Find First and Last Position of Element in Sorted Array
  • 原文地址:https://www.cnblogs.com/zimengxiyu/p/16360459.html
Copyright © 2020-2023  润新知