C# 七牛 html 上传图片,是否能指定大小 比如 100px*100px

油菜
  • 61

七牛 html 上传图片,是否能指定大小 比如 100px*100px ?

尺寸不对 返回错误!
代码如下
====================后台===================

        Config.ACCESS_KEY = "---";
        Config.SECRET_KEY = "--";
        string Bucket = "test";
        string DOMAIN = "img.test.com";
        string result = "0";  //上传失败
        string retStr = Request["upload_ret"];

        if (string.IsNullOrEmpty(retStr))
        {
            string code = Url.Encode(Request["code"]);
            string errMsg = Url.Encode(Request["error"]);
            if (string.IsNullOrEmpty(errMsg)) result = "";   //未开始上传
            PutPolicy policy = new PutPolicy(Bucket, 3600);
            string address = ConfigurationManager.AppSettings["ProjectAddress"].ToString();
            policy.ReturnUrl = "" + address + "/School/UploadLogo";
            policy.CallBackBody = "w=$(100)&h=$(100)";
            string upToken = policy.Token();
            DateTime time = DateTime.Now;
            string picName = AuthenticationService.Session.User.School.Code + "_logo_" + time.Year + time.Month + time.Day + time.Second + ".jpg";
            ViewBag.upToken = upToken;
            ViewBag.pictureName = picName;

        }
        else
        {
            try
            {
                string s = ASCIIEncoding.Default.GetString(Convert.FromBase64String(retStr));
                Dictionary<string, object> str = new JavaScriptSerializer().Deserialize<Dictionary<string, object>>(s);
                object keyValue = "";
                str.TryGetValue("key", out keyValue);
                string baseUrl = GetPolicy.MakeBaseUrl(DOMAIN, keyValue.ToString());
                string privateUrl = GetPolicy.MakeRequest(baseUrl);
                schoolService.UpdateSchoolLogo(AuthenticationService.Session.User.SchoolId, privateUrl);
                result = "1";
            }
            catch { 

            }
        }

        ViewBag.result = result;

=================================

执行到这句代码报错: ”CallBackUrl and CallBackBody error“

string upToken = policy.Token();
回复
阅读 3.9k
1 个回答

这个可以在业务服务器的 callback 里面进行限制。用户上传图片的时候,七牛的上传凭证(uptoken)里面,指定 callbackUrl,并设置 callbackBody = "w=$(imageInfo.width)&h=$(imageInfo.height)" 这样你们业务服务器收到这个 callback 后,判断 w 和 h 如果不是 100 则返回错误(http status code 为 非 200),这样就拒绝了此类上传。