Click here to Skip to main content
15,885,939 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys! I'm seriously struggling several days, so any help would be appreciable
So. Here is reference for documentation[^]
I've tried such approach
C#
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
C#
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
C#
[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!
Posted
Updated 6-Jan-14 4:18am
v3
Comments
ZurdoDev 6-Jan-14 9:28am    
You may want to post on the site where you got this from.
Bohdan Stupak 6-Jan-14 10:19am    
I'm not actually sure what do you mean, but I've updated the question to show, how I actually use my method
Bohdan Stupak 6-Jan-14 10:30am    
I've already seen it. And provided link in my question for everyone, who is unfamiliar with imageshack API. And it seems to me, that I've filled all mandatory parameters that are described in paragraph about XML API in my method. But still I get errors. That's why I've posted this question
ZurdoDev 6-Jan-14 10:30am    
I know. My point is you may get a quicker and more accurate response if you also post there.

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900