zoukankan      html  css  js  c++  java
  • uniapp 上传图片至阿里云OSS

    本文出自:https://www.cnblogs.com/2186009311CFF/p/14248337.html

    uniapp可参考页面插件 : https://ext.dcloud.net.cn/plugin?id=3814

    //oss参数请求接口获得
                uploadImage: function(imageSrc_local, OssPara, userId) {
                    return new Promise((resolve, reject) => {
                        
                        //当前时间精确到毫秒字符串
                        var date = new Date(); //获取时间
                        //console.log(date);
                        var year = date.getFullYear();
                        var month = date.getMonth() + 1;
                        var day = date.getDate();
    
                        var hour = date.getHours();
                        var minute = date.getMinutes();
                        var second = date.getSeconds();
    
                        month = isSubTen(month);
                        day = isSubTen(day);
                        //这样写显示时间在1~9会挤占空间;所以要在1~9的数字前补零;
                        if (hour < 10) {
                            hour = '0' + hour;
                        }
                        if (minute < 10) {
                            minute = '0' + minute;
                        }
                        if (second < 10) {
                            second = '0' + second;
                        }
                        //var x=date.getDay();//获取星期    
                        var time = year + month + day + hour + minute + second;
    
    
    
                        //图片命名规则:接口返回的地址 +接口返回的前缀 + 用户id +'_'+当前时间精确到毫秒字符串 +'.png';
    
                        let nameStr = OssPara.dir + userId + '_' + date + '.png';
                        console.log('nameStr' + nameStr) uni.uploadFile({
                            url: OssPara.host, //输入你的bucketname.endpoint  
                            filePath: imageSrc_local,
                            fileType: 'image',
                            name: 'file',
                            formData: {
                                name: nameStr,
                                key: nameStr,
                                policy: OssPara.policy, // 输入你获取的的policy 
                                OSSAccessKeyId: OssPara.accessKeyId, // 输入你的AccessKeyId 
                                success_action_status: '200', // 让服务端返回200,不然,默认会返回204 
                                signature: OssPara.signature, // 输入你获取的的signature  
                            },
                            success: res => {
                                //console.log(res) 
                                if (res.statusCode == '200') {
                                    resolve(OssPara.host + '/' + nameStr);
                                } else {
                                    console.log.log(res);
                                    reject('接口返回错误');
                                }
                            },
                            fail() {
                                reject('网络链接错误');
                            }
                        });
                    });
                },
  • 相关阅读:
    16 Errors and Exceptions
    13 Asynchronous Programming
    wpf入门
    iframe cross domain
    ini_set("error_log",$error_path)设置
    MySQL中merge表存储引擎用法
    php中静态属性静态方法的用法
    PHP的SESSIONID生成原理
    composer的基本 使用
    相邻省份数组
  • 原文地址:https://www.cnblogs.com/2186009311CFF/p/14248337.html
Copyright © 2011-2022 走看看