Export to GitHub

imageshackapi - issue #47

Errors uploading file using C#


Posted on Jan 6, 2014 by Massive Bear

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!

Status: New

Labels:
Type-Defect Priority-Medium