Question

im trying to develope C# application that uses Imgur api to upload images.

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. but when im uploding bigger images (500kb) i get the error: Invalid URI: The Uri string is too long.

what can i do?

Was it helpful?

Solution

i succeed to upload large images by dividing the base64 string

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.

OTHER TIPS

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.

You will not be able to get around this issue with a single URI, as its inherent in imgur's design.

Read this for the maximum length a URI can be: What is the maximum length of a URL in different browsers?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top