React使用antd的upload组件上传图片到阿里云,服务端签名

作者 : 开心源码 本文共2442个字,预计阅读时间需要7分钟 发布时间: 2022-05-13 共255人阅读

在React框架中使用antd组件里面upload组件上传图片到阿里云。
antd有官方文档,但是有点复杂了,实际上用不了那么多的方法,我简化了一下。

我使用服务端签名,前台传。
首先是后台代码

  public static Map<String,String> getSignatureAndPolicy(){        Map<String, String> returnMap = new HashMap<>();        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);        long expireTime = 3600;//过期时间加一天        long expireEndTime = System.currentTimeMillis() + expireTime * 1000*24;        Date expiration = new Date(expireEndTime);        PolicyConditions policyConds = new PolicyConditions();        policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, 1048576000);        String postPolicy = ossClient.generatePostPolicy(expiration, policyConds);        byte[] binaryData = new byte[0];        try {            binaryData = postPolicy.getBytes("utf-8");        } catch (UnsupportedEncodingException e) {            e.printStackTrace();        }        String encodedPolicy = BinaryUtil.toBase64String(binaryData);        String postSignature = ossClient.calculatePostSignature(postPolicy);        returnMap.put("signature",postSignature);        returnMap.put("policy",encodedPolicy);        return returnMap;    }

获取到了policy和signature,接下来是前台代码

const GLOBAL_VAR={ossUploadUrl:bucketname+endpoint ,//例如 http://bucketname.oss-cn-chengdu.aliyuncs.comaccessKeyId:accessKeyId,//你的阿里云accessKeyId}class UploadImgForm extends React.Component{    constructor(props){        super(props)        this.state = {            fileList:[],            ossData:new Object()        }    }    componentDidMount() {        this.getSignatureAndPolicy()    }    getSignatureAndPolicy=()=>{//这是向后端发送请求,获取policy和signature        Ajax.post("bizActivity/getSignatureAndPolicy",{},{},(xhr,data)=>{            let ossData = this.state            ossData.policy = data.data.policy            ossData.signature = data.data.signature            ossData.expire = new Date().getTime()            ossData.host = GLOBAL_VAR.ossUploadUrl            ossData.accessId = GLOBAL_VAR.accessKeyId            this.setState({ossData})        })    }    getExtraData = file => {        const { ossData } = this.state;        return {            key:file.name,            OSSAccessKeyId: ossData.accessId,            policy: ossData.policy,            Signature: ossData.signature,            success_action_status:200        };    }    render(){        const { getFieldDecorator, getFieldValue } = this.props.form;        const { stepList, activityId } = this.props        const formItemLayout = {            labelCol: { span: 5 },            wrapperCol: { span: 16 }        };//注意这里的name的值不要是multiplefile,会请求失败        const props = {            name: 'file',            action:GLOBAL_VAR.ossUploadUrl,            multiple:true,            showUploadList:{                showPreviewIcon: false, showRemoveIcon: false, showDownloadIcon: false            },            listType:"picture",            data: this.getExtraData,            fileList:this.state.fileList,            onChange: ({ fileList,file }) => {                this.setState({ fileList:fileList })            },        }        return (<Form>            <Form.Item {...formItemLayout} label="上传图片">                <Upload {...props} >                    <Button >上传图片</Button>                </Upload>            </Form.Item>        </Form>)    }}

就是这样简单啦,有问题的可以评论大家探讨下哦。

说明
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » React使用antd的upload组件上传图片到阿里云,服务端签名

发表回复