codeminders / imageshackapi

Automatically exported from code.google.com/p/imageshackapi
0 stars 0 forks source link

Errors uploading file using C# #47

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
I'm using such method to upload image via your XML API
public static void UploadImage(HttpPostedFileBase image)
        {
            var httpWReq =
                (HttpWebRequest)WebRequest.Create("http://www.imageshack.us/upload_api.php");

            httpWReq.Method = "POST";
            httpWReq.ContentType = "multipart/form-data";
            httpWReq.KeepAlive = true;

            var stream = httpWReq.GetRequestStream();
            var encoding = new UTF8Encoding();
            var postData = "key=/*my key*/";
            postData += "&fileupload=";
            byte[] data = encoding.GetBytes(postData);
            stream.Write(data, 0, data.Length);
            var buffer = new byte[4096];
            int bytesread;
            while ((bytesread = image.InputStream.Read(buffer, 0, buffer.Length)) != 0)
            {
                stream.Write(buffer, 0, bytesread);
            }
            stream.Close();

            var response = (HttpWebResponse)httpWReq.GetResponse();

            string responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
}
but it gives me error "you must provide valid secret key". The problem is that 
key is 100% valid. And when I change to
httpWReq.ContentType = "application/x-www-form-urlencoded";
there is no longer error with key, but there is error "no file or empty file 
was uploaded".
Here's some sample code, how I use this method
[HttpPost] 
public ActionResult SomeAction (SomeViewModel model)
{
    //I take image uploaded by user and store it into imageshack
    UploadeImage(model.Image);
    RedirectToAction("Index");
}
So the question is: Are there any mistakes in this code or is there any more 
descent approach to post image via this API?
Thanks in advance! 

Original issue reported on code.google.com by BohdanSt...@gmail.com on 6 Jan 2014 at 4:24