分享一個react 圖片上傳組件 支持OSS 七牛雲


react-uplod-img 是一個基於 React antd組件的圖片上傳組件 支持oss qiniu等服務端自定義獲取簽名,批量上傳, 預覽, 刪除, 排序等功能

需要 react 版本大於 v16.1.0 支持async await

git地址

demo

安裝

npm i react-uplod-img --save

引入

import UpImage from 'react-uplod-img'

調用

const getOSSSign = (suffix,width,height, extraParam) => {
    const apiServerUrl = 'https://hp.bncry.cn/util/getAliyunSignature';
    const url = `${apiServerUrl}?${[
        `bizName=${extraParam.bizName}`,
        `suffix=${suffix}`,
        `width=${width}`,
        `height=${height}`,
    ].join('&')}`;

    return new Promise((resolve,reject)=>{
        fetch(url).then(async (response)=>{
            const res = await response.json();
            const formData = res.data;
            resolve({
                key: formData.dirPath,
                policy: formData.policy,
                OSSAccessKeyId: formData.OSSAccessKeyId,
                success_action_status: '200',
                callback: formData.callback,
                signature: formData.signature,
            });
        })
    })
};

const ossUploadConfig = {
            type:'oss',
            imageUploadServerHost: 'https://hp-file-lf.oss-cn-hangzhou.aliyuncs.com', //圖片上傳服務地址
            imageShowServiceHost: 'https://hp-file-lf.oss-cn-hangzhou.aliyuncs.com', // 圖片查看地址前綴
            totalNum: 5,
            supportSort: true,
            value:'avatar/2018-10-10/f2b3ace0-cc33-11e8-8ad4-3550e70cc242_220_138.jpg;avatar/2018-10-10/f2b42210-cc33-11e8-8ad4-3550e70cc242_1080_1920.jpg;avatar/2018-10-10/f2b44920-cc33-11e8-8ad4-3550e70cc242_1280_719.jpg'
        };

<UpImage getSign={getOSSSign}  {...ossUploadConfig} extraParam={{bizName:"avatar"}}/>
配置項 類型 默認值 描述
type String oss 類型 目前支持 oss qiniu
imageUploadServerHost String 圖片上傳服務地址前綴
imageShowServiceHost String 圖片查看服務地址前綴
maxSize Number 2048 圖片大小限制 單位KB
totalNum Number 1 圖片數量限制
supportSort Bool false 是否支持拖拽排序
extraParam Object 獲取簽名getSign方法 的第四個參數 供獲取簽名時 自定義入參
getSign Func (suffix,width,height, extraPara)=>{} 獲取簽名的方法 Promise
onChange Func (values)=>{} 圖片上傳成功時的回調 參數為圖片的半路徑;分隔的一個字符串
value String 回顯圖片的路徑 半路徑 ;分隔

getSign

suffix 圖片后綴 width 圖片寬度 height 圖片高度 extraParam 自定義的參數

width 和 height 參數是組件通過渲染獲取的圖片真實寬高,
推薦傳遞到服務器端參與簽名 生成的URL key中能攜帶寬高信息 如
/avatar/2018-10-10/f2b3ace0-cc33-11e8-8ad4-3550e70cc242_800_600.jpg 圖片路徑中攜帶了寬高信息 后期前端頁面圖片懶加載時 可以通過鏈接中的寬高信息做優化

    // oss
    const getSign = (suffix,width,height, extraParam) => {
        const apiServerUrl = 'https://hp.bncry.cn/util/getAliyunSignature';
        const url = `${apiServerUrl}?${[
            `bizName=${extraParam.bizName}`,
            `suffix=${suffix}`,
            `width=${width}`,
            `height=${height}`,
        ].join('&')}`;

        return new Promise((resolve,reject)=>{
            fetch(url).then(async (response)=>{
                const res = await response.json();
                const formData = res.data;
                resolve({
                    key: formData.dirPath,
                    policy: formData.policy,
                    OSSAccessKeyId: formData.OSSAccessKeyId,
                    success_action_status: '200',
                    callback: formData.callback,
                    signature: formData.signature,
                });
            })
        })
    };

    // qiniu

    const getSign = (suffix,width,height, extraParam) => {
        const qiniuApiServerUrl = 'https://hp.bncry.cn/util/getQiniuSignature';
        const url = `${qiniuApiServerUrl}?${[
            `suffix=${suffix}`,
            `width=${width}`,
            `height=${height}`,
        ].join('&')}`;

        return new Promise((resolve,reject)=>{
            fetch(url).then(async (response)=>{
                const formData = await response.json();
                resolve({
                    token: formData.uptoken,
                });
            })
        })
    };

注意事項

獲取簽名采用的是async await的異步處理方式 需要你的項目支持async await 如果不支持 會報 ReferenceError: regeneratorRuntime is not defined

解決方案

npm i --save-dev babel-plugin-transform-runtime
在 .babelrc 文件中添加:
"plugins": [[
    "transform-runtime",
    {
      "helpers": false,
      "polyfill": false,
      "regenerator": true,
      "moduleName": "babel-runtime"
    }
  ]]


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM