简体   繁体   English

将图像上传到imgur会导致:Uri字符串太长

[英]Uploading image to imgur causes: The Uri string is too long

im trying to develope C# application that uses Imgur api to upload images. 我试图开发使用Imgur api上传图像的C#应用​​程序。

this is my upload function: 这是我的上传功能:

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

if im uploading small pictures (20kb) all works fine. 如果即时上传小图片(20kb)一切正常。 but when im uploding bigger images (500kb) i get the error: Invalid URI: The Uri string is too long. 但当我上传更大的图像(500kb)时,我得到错误:无效的URI:Uri字符串太长。

what can i do? 我能做什么?

i succeed to upload large images by dividing the base64 string 我通过划分base64字符串成功上传大图像

this is the working code: 这是工作代码:

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

thanks to all. 谢谢大家。

They way it works is the image is converted like so: System.Convert.ToBase64String(imageData) That is added to the URI and sent to the server to be decoded back to an image. 它们的工作方式是图像转换如下: System.Convert.ToBase64String(imageData)它被添加到URI并发送到服务器以解码回图像。

You will not be able to get around this issue with a single URI, as its inherent in imgur's design. 您将无法使用单个URI解决此问题,因为它是imgur设计中固有的。

Read this for the maximum length a URI can be: What is the maximum length of a URL in different browsers? 读取此内容可获得URI 的最大长度:不同浏览器中URL的最大长度是多少?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM