摘要:是一個(gè)基于組件的圖片上傳組件支持等服務(wù)端自定義獲取簽名批量上傳預(yù)覽刪除排序等功能需要版本大于支持地址安裝引入調(diào)用圖片上傳服務(wù)地址圖片查看地址前綴配置項(xiàng)類型默認(rèn)值描述類型目前
react-uplod-img 是一個(gè)基于 React antd組件的圖片上傳組件 支持oss qiniu等服務(wù)端自定義獲取簽名,批量上傳, 預(yù)覽, 刪除, 排序等功能
需要 react 版本大于 v16.1.0 支持async await
git地址
demo
安裝
npm i react-uplod-img --save
引入
import UpImage from "react-uplod-img"
調(diào)用
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", //圖片上傳服務(wù)地址 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" };
配置項(xiàng) | 類型 | 默認(rèn)值 | 描述 |
---|---|---|---|
type | String | oss | 類型 目前支持 oss qiniu |
imageUploadServerHost | String | 圖片上傳服務(wù)地址前綴 | |
imageShowServiceHost | String | 圖片查看服務(wù)地址前綴 | |
maxSize | Number | 2048 | 圖片大小限制 單位KB |
totalNum | Number | 1 | 圖片數(shù)量限制 |
supportSort | Bool | false | 是否支持拖拽排序 |
extraParam | Object | 獲取簽名getSign方法 的第四個(gè)參數(shù) 供獲取簽名時(shí) 自定義入?yún)?/td> | |
getSign | Func | (suffix,width,height, extraPara)=>{} | 獲取簽名的方法 Promise |
onChange | Func | (values)=>{} | 圖片上傳成功時(shí)的回調(diào) 參數(shù)為圖片的半路徑;分隔的一個(gè)字符串 |
value | String | 回顯圖片的路徑 半路徑 ;分隔 |
suffix 圖片后綴 width 圖片寬度 height 圖片高度 extraParam 自定義的參數(shù)
width 和 height 參數(shù)是組件通過渲染獲取的圖片真實(shí)寬高,
推薦傳遞到服務(wù)器端參與簽名 生成的URL key中能攜帶寬高信息 如
/avatar/2018-10-10/f2b3ace0-cc33-11e8-8ad4-3550e70cc242_800_600.jpg 圖片路徑中攜帶了寬高信息 后期前端頁面圖片懶加載時(shí) 可以通過鏈接中的寬高信息做優(yōu)化
// 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, }); }) }) };
注意事項(xiàng)
獲取簽名采用的是async await的異步處理方式 需要你的項(xiàng)目支持async await 如果不支持 會(huì)報(bào) 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" } ]]
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/98268.html
摘要:接下來講解一下博客中用的圖床阿里云對象存儲(chǔ),簡稱,雖然目前大家用的比較多的是七牛云之類的,但是因?yàn)槲覀€(gè)人原因我還是選擇了阿里云,后面我會(huì)簡單說明下原因。 showImg(https://segmentfault.com/img/remote/1460000008838754?w=960&h=300); 前言 之前打算寫一篇有關(guān)個(gè)人博客SEO優(yōu)化的體驗(yàn)和一些自身體會(huì),但是發(fā)現(xiàn)自己還沒完全...
摘要:接下來講解一下博客中用的圖床阿里云對象存儲(chǔ),簡稱,雖然目前大家用的比較多的是七牛云之類的,但是因?yàn)槲覀€(gè)人原因我還是選擇了阿里云,后面我會(huì)簡單說明下原因。 showImg(https://segmentfault.com/img/remote/1460000008838754?w=960&h=300); 前言 之前打算寫一篇有關(guān)個(gè)人博客SEO優(yōu)化的體驗(yàn)和一些自身體會(huì),但是發(fā)現(xiàn)自己還沒完全...
閱讀 3468·2023-04-26 00:39
閱讀 4077·2021-09-22 10:02
閱讀 2560·2021-08-09 13:46
閱讀 1108·2019-08-29 18:40
閱讀 1457·2019-08-29 18:33
閱讀 784·2019-08-29 17:14
閱讀 1525·2019-08-29 12:40
閱讀 2985·2019-08-28 18:07