Search code examples
c#uploadingimgur

uploading image to imgur using c#


Uploading image to imgur.com using code below returns http 400 error code. My developer key is correct and I tried different image formats with sizes upto 70 kb. I also tried code example for c# given at http://api.imgur.com/examples but it also gives http 400. What might be the problem?

public XDocument Upload(string imageAsBase64String)
{
    XDocument result = null;
    using (var webClient = new WebClient())
    {
        var values = new NameValueCollection
        {
            { "key", key },
            { "image", imageAsBase64String },
            { "type", "base64" },
        };
        byte[] response = webClient.UploadValues("http://api.imgur.com/2/upload.xml", "POST", values);
        result = XDocument.Load(new MemoryStream(response));
    }
    return result;
}

EDIT: This is an ASP.NET MVC application and caller controller action is:

[HttpPost]
public ActionResult UploadImage(HttpPostedFileBase uploadFile)
{
    if (uploadFile.ContentLength > 0)
    {
        var imgService = new ImgUrImageService();
        byte[] fileBytes = new byte[uploadFile.InputStream.Length];
        Int64 byteCount = uploadFile.InputStream.Read(fileBytes, 0, (int)uploadFile.InputStream.Length);
        uploadFile.InputStream.Close();
        string fileContent = Convert.ToBase64String(fileBytes, 0, fileBytes.Length);
        var response = imgService.Upload(fileContent);
    }
    return View();
}

Solution

  • Ok I found the reason. A proxy setting (for Fiddler) in my web.config file was causing the issue. Removing it solved the problem and my another issue too (related to recaptcha). Code is working like a charm.