如何使用imgur api C#上传大尺寸图像文件? [英] How to upload big size image file using imgur api C#?

查看:77
本文介绍了如何使用imgur api C#上传大尺寸图像文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个关于imgur上传大尺寸文件的问题。

我找到了一个使用api

它适用于小尺寸图像文件,但它不适用于大尺寸图像文件

我已经测试了488kb大小的图像文件。

错误返回400错误请求服务器错误。

这是我的源代码,请给我一个建议

谢谢



Hi,
I have a question about upload big size file on imgur.
I found a source code for uploading images on imgur.com using api
It works for small size image file but it doesn't work for big size image file
I have tested with 488kb size image file.
The error returns 400 bad request server error.
Here is my source code and please give me an advice
Thank you

private string upload_img(string img_name)
       {
           string img_url = "";
           try
           {
               string api_url_image = "https://api.imgur.com/3/image";
             //  string api_url = "https://api.imgur.com/3/upload";

               HttpWebRequest request = (HttpWebRequest)WebRequest.Create(api_url_image);
               request.Headers.Add("Authorization", "Client-ID <<My client ID Here>>");    //authorized id
          

               request.Method = "POST";
Folder name\\" + img_name;

               FileStream file = new FileStream(filePath, FileMode.Open);
               byte[] image = new byte[file.Length];
               file.Read(image, 0, (int)file.Length);
               ASCIIEncoding enc = new ASCIIEncoding();
               string postData = Convert.ToBase64String(image);
               byte[] bytes = enc.GetBytes(postData);

               request.ContentType = "application/x-www-form-urlencoded";
               request.ContentLength = bytes.Length;

               Stream writer = request.GetRequestStream();
               writer.Write(bytes, 0, bytes.Length);

               HttpWebResponse response = (HttpWebResponse)request.GetResponse();
               response.GetResponseStream();

               if (response.StatusCode == HttpStatusCode.OK)
               {

                   Stream responseStream = response.GetResponseStream();
                   string responseStr = new StreamReader(responseStream).ReadToEnd();

                   img_url = responseStr.Remove(0, responseStr.IndexOf("link") + 7);
                   img_url = img_url.Substring(0, img_url.IndexOf("success") - 4);
                   img_url = img_url.Replace("\\","");
                   responseStream.Close();
               }
               response.Close();
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }
         
           return img_url;
       }





我的尝试:



我已将api密钥从匿名密钥更改为授权密钥,但它返回相同的错误



What I have tried:

I have changed the api key to authorized key from anonymous key but it returns same error

推荐答案

问题是我限制了我下载时的文件大小
The problem was I limited the file size when I download it


您正在尝试上传表单数据,但使用的是URL编码。检查您的内容类型。
You're trying to upload form data, but using URL encoding. Check your content type.


这篇关于如何使用imgur api C#上传大尺寸图像文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆