Quickblox在c#中上传文件rest api调用,给出400

本文关键字:调用 api 给出 rest 文件 Quickblox | 更新日期: 2023-09-27 18:26:14

我正在使用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中给了我400个糟糕的请求。上传值()。。它在休息客户(邮差)中运行良好。

请帮助

Quickblox在c#中上传文件rest api调用,给出400

与其使用web客户端,不如使用http客户端并作为MultipartFormDataContent发送。它对我有用!!!!

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;
                }
            }
        }