在 c# 中 Quickblox 上传文件休息 api 调用给出 400

Posted

技术标签:

【中文标题】在 c# 中 Quickblox 上传文件休息 api 调用给出 400【英文标题】:Quickblox Upload file rest api call in c# giving 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 错误请求。它在休息客户端(邮递员)中运行良好。

请帮忙

【问题讨论】:

【参考方案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;
                
            
        

【讨论】:

以上是关于在 c# 中 Quickblox 上传文件休息 api 调用给出 400的主要内容,如果未能解决你的问题,请参考以下文章

无需 SDK 将文件上传到 Quickblox,最好使用 Alamofire

Quickblox 内容与自定义对象文件

无法使用 node.js 在 quickblox 上上传内​​容

在 quickblox 上上传个人资料图片时 Swift 出错

如何从 HTML 页面使用 apache 骆驼休息端点上传 txt 文件

QuickBlox - 如何使用 REST API + PHP 创建 blob 内容