1

我正在尝试开发使用 Imgur api 上传图像的 C# 应用程序。

这是我的上传功能:

    public static string PostToImgur(string imagFilePath, string apiKey)
{
    byte[] imageData;

    FileStream fileStream = File.OpenRead(imagFilePath);
    imageData = new byte[fileStream.Length];
    fileStream.Read(imageData, 0, imageData.Length);
    fileStream.Close();

    string uploadRequestString = "image=" + Uri.EscapeDataString(System.Convert.ToBase64String(imageData)) + "&key=" + apiKey;

    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("https://api.imgur.com/3/image");
    webRequest.Method = "POST";
    webRequest.ContentType = "application/x-www-form-urlencoded";
    webRequest.ServicePoint.Expect100Continue = false;
    webRequest.Headers["Authorization"] = "Client-ID abd937cc5e11dc9";

    StreamWriter streamWriter = new StreamWriter(webRequest.GetRequestStream());
    streamWriter.Write(uploadRequestString);
    streamWriter.Close();

    WebResponse response = webRequest.GetResponse();
    Stream responseStream = response.GetResponseStream();
    StreamReader responseReader = new StreamReader(responseStream);

    return responseReader.ReadToEnd();
}

如果我上传小图片(20kb)一切正常。但是当我上传更大的图像(500kb)时,我收到错误:无效的 URI:Uri 字符串太长。

我能做些什么?

4

2 回答 2

2

我通过分割base64字符串成功上传大图像

这是工作代码:

    public static string PostToImgur(string imagFilePath, string apiKey)
{
    byte[] imageData;

    FileStream fileStream = File.OpenRead(imagFilePath);
    imageData = new byte[fileStream.Length];
    fileStream.Read(imageData, 0, imageData.Length);
    fileStream.Close();

    const int MAX_URI_LENGTH = 32766;
    string base64img = System.Convert.ToBase64String(imageData);
    StringBuilder sb = new StringBuilder();

    for (int i = 0; i < base64img.Length; i += MAX_URI_LENGTH)
    {
        sb.Append(Uri.EscapeDataString(base64img.Substring(i, Math.Min(MAX_URI_LENGTH, base64img.Length - i))));
    }

    string uploadRequestString = "key=" + apiKey + "&title=" + "imageTitle" +
        "&caption=" + "img" + "&image=" + sb.ToString();

   // string uploadRequestString = "image=" + Uri.EscapeDataString(System.Convert.ToBase64String(imageData)) + "&key=" + apiKey;

    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("https://api.imgur.com/3/image");
    webRequest.Method = "POST";
    webRequest.ContentType = "application/x-www-form-urlencoded";
    webRequest.ServicePoint.Expect100Continue = false;
    webRequest.Headers["Authorization"] = "Client-ID abd937cc5e11dc9";

    StreamWriter streamWriter = new StreamWriter(webRequest.GetRequestStream());
    streamWriter.Write(uploadRequestString);
    streamWriter.Close();

    WebResponse response = webRequest.GetResponse();
    Stream responseStream = response.GetResponseStream();
    StreamReader responseReader = new StreamReader(responseStream);

    return responseReader.ReadToEnd();
}

谢谢大家。

于 2013-04-28T21:50:11.467 回答
1

他们的工作方式是将图像转换为这样: System.Convert.ToBase64String(imageData) 将其添加到 URI 并发送到服务器以解码回图像。

您将无法使用单个 URI 解决此问题,因为它是 imgur 设计中固有的。

阅读此内容以了解 URI 的最大长度:不同浏览器中 URL 的最大长度是多少?

于 2013-04-28T20:56:07.647 回答