【问题标题】:Quickblox Upload file rest api call in c# giving 400在 c# 中 Quickblox 上传文件休息 api 调用给出 400
【发布时间】:2015-12-08 12:29:40
【问题描述】:

我正在使用api上传头像...参考链接:http://quickblox.com/developers/Content#API_Content_Upload_File

var uri = new Uri(getparam);
                                var query = HttpUtility.ParseQueryString(uri.Query);
                                var ContentType = query.Get("Content-Type");
                                var Expires = query.Get("Expires");
                                var acl = query.Get("acl");
                                var key = query.Get("key");
                                var policy = query.Get("policy");
                                var successactionstatus = query.Get("success_action_status");
                                var xamzalgorithm = query.Get("x-amz-algorithm");
                                var xamzcredential = query.Get("x-amz-credential");
                                var xamzdate = query.Get("x-amz-date");
                                var xamzsignature = query.Get("x-amz-signature");                              

                                string profileimagepath = Path.Combine(HttpContext.Current.Server.MapPath("~/Content/ProfileImage/"));
                                var fullpath=profileimagepath + newFileName;
                                var fileName = System.IO.Path.GetFileName(Convert.ToString(fullpath));


                                using (var wb = new WebClient())
                                {

                                    var data = new NameValueCollection();
                                    data["Content-Type"] = ContentType;
                                    data["Expires"] = Expires;
                                    data["acl"] = acl;
                                    data["key"] = key;
                                    data["policy"] = policy;
                                    data["success_action_status"] = successactionstatus;`enter code here`
                                    data["x-amz-algorithm"] = xamzalgorithm;
                                    data["x-amz-credential"] = xamzcredential;
                                    data["x-amz-date"] = xamzdate;
                                    data["x-amz-signature"] = xamzsignature;
                                    //data["file"] = ImageToBase64(fullpath);
                                    data["file"] = @filename;
                                     wb.UploadFile("https://qbprod.s3.amazonaws.com/", "POST", fullpath);

                                    var qbUploadFileResponse = wb.UploadValues("https://qbprod.s3.amazonaws.com/", data);

                                }

它在 wb.UploadValues() 中给了我 400 错误请求。它在休息客户端(邮递员)中运行良好。

请帮忙

【问题讨论】:

    标签: c# rest webclient quickblox bad-request


    【解决方案1】:

    您应该使用 http 客户端并作为 MultipartFormDataContent 发送,而不是使用 Web 客户端。它对我有用!!!!

    HttpContent bytesContent = new ByteArrayContent(ImageToBase64(fullpath));
                                    using (var client = new HttpClient())
                                    using (var formData = new MultipartFormDataContent())
                                    {
    
                                        formData.Add(new StringContent(ContentType), "Content-Type");
                                        formData.Add(new StringContent(Expires), "Expires");
                                        formData.Add(new StringContent(acl), "acl");
                                        formData.Add(new StringContent(key), "key");
                                        formData.Add(new StringContent(policy), "policy");
                                        formData.Add(new StringContent(successactionstatus), "success_action_status");
                                        formData.Add(new StringContent(xamzalgorithm), "x-amz-algorithm");
                                        formData.Add(new StringContent(xamzcredential), "x-amz-credential");
                                        formData.Add(new StringContent(xamzdate), "x-amz-date");
                                        formData.Add(new StringContent(xamzsignature), "x-amz-signature");
                                        formData.Add(bytesContent, "file", fileName);
                                        var responseTemp = client.PostAsync("https://qbprod.s3.amazonaws.com/", formData).Result;
                                        if (!responseTemp.IsSuccessStatusCode)
                                        {
                                            resultResponse.ErrorCode = "0";
                                            resultResponse.Message = "Profile Save Succesfully. But Quick Bolx Blob A/c Not Updated";
                                            resultResponse.Data = response.Response;
                                            return Ok(resultResponse);
                                        }
                                        var respon = responseTemp.Content.ReadAsStreamAsync().Result;
    
                                        if (respon != null)
                                        {
                                            var finalResponse = DeclaringFileUpload(gettokenbyuser, Quickbolxblob_id);
    
    
    ////// convert image in Base64 byte.
     public byte[] ImageToBase64(string path)
            {
                using (Image image = Image.FromFile(path))
                {
                    using (MemoryStream m = new MemoryStream())
                    {
                        image.Save(m, image.RawFormat);
                        byte[] imageBytes = m.ToArray();
    
                        // Convert byte[] to Base64 String
                        string base64String = Convert.ToBase64String(imageBytes);
                        return imageBytes;
                    }
                }
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-10
      • 1970-01-01
      • 2015-12-27
      • 1970-01-01
      • 1970-01-01
      • 2019-12-28
      • 2020-07-27
      • 2016-05-23
      相关资源
      最近更新 更多