We all started somewhere! Share your experience on the Get Advice: Let's Get Started Sweepstakes thread and be entered to win a $100 credit on your Constant Contact account.

Help with Uploading an Image

heinscott
Participant

Help with Uploading an Image

 Hello,


I'm trying to upload an image file using an HttpWebRequest and am having a difficult time.  Seems that no matter what I try, I am recieving a 400 Error (Bad Request). Could anyone possibly help me figure out where the request is not valid?  Here is the code I am using (C#).


 


        string st = "";


        string sUri = ("https://api.constantcontact.com/ws/customers/" + sUsername + "/library/folders/" + folderid + "/images");


        Uri address = new Uri(sUri);


 


        HttpWebRequest request = (WebRequest.Create(address)) as HttpWebRequest;


        request.Credentials = new NetworkCredential((sAPIKey + ("%" + sUsername)), sPassword);


 


        request.Method = "POST";


        request.ContentType = "multipart/form-data";


        byte[] filedata = null;


        using (BinaryReader reader = new BinaryReader(File.OpenRead("test.jpg")))


        {


            filedata = reader.ReadBytes((int)reader.BaseStream.Length);


        }


 


        byte[] tmpHash = new MD5CryptoServiceProvider().ComputeHash(filedata);


        StringBuilder sb = new StringBuilder(tmpHash.Length);


        for (int i = 0; i < tmpHash.Length; i++)


        {


            sb.Append(tmpHash.ToString("X2"));


        }


        StringBuilder data = new StringBuilder();


        data.AppendLine("Content-Disposition: form-data; Content-Type: application/atom+xml");


        data.AppendLine("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>");


        data.AppendLine("<atom:entry xmlns:atom=\"http://www.w3.org/2005/Atom\">");


        data.AppendLine(" <atom:content>");


        data.AppendLine("  <Image>");


        data.AppendLine("   <FileName>test.jpg</FileName>");


        data.AppendLine("   <MD5Hash>" + sb.ToString() + "</MD5Hash>");


        data.AppendLine("   <Description />");


        data.AppendLine("  </Image>");


        data.AppendLine(" </atom:content>");


        data.AppendLine("</atom:entry>");


        data.AppendLine();


        data.AppendLine("Content-Disposition: form-data; Content-Transfer-Encoding: binary; name=\"file\"; filename=\"test.jpg\"; Content-Type: image/jpg");


        byte[] byteArray = Encoding.UTF8.GetBytes(data.ToString());


        request.ContentLength = byteArray.Length + filedata.Length;


        Stream RequestStream = request.GetRequestStream();


        RequestStream.Write(byteArray, 0, byteArray.Length);


        RequestStream.Write(filedata, 0, filedata.Length);


        try 


        {


            HttpWebResponse response = request.GetResponse() as HttpWebResponse;


            StreamReader reader2 = new StreamReader(response.GetResponseStream());


            st = reader2.ReadToEnd();


            response.Close();


        }


        catch (Exception ex)


        {


            st = ex.Message.ToString();


        }


        return st;


 


Thanks for any help you can give me.


 


Scott


 

1 REPLY 1
DaveBerard
Moderator

Hi Scott,


 


Good question on this and it's something we're both looking to add to the C#.NET Wrapper Library and also put up a blog post on how to do (it's definitely more complicated than sending XML).  I did do a blog post on how to do this on our Labs website including a code sample for how to do this in a C# Form Application.  Code is resable in any other application or website, simply would need to include the 3 core .cs files into your application.  You can find the blog post and sample code here: www.ctctlabs.com/index.php/blog/detail/image_hosting_and_the_constant_contact_api/

Dave Berard
Senior Product Manager, Constant Contact
Developer Portal

View API documentation, code samples, get your API key.

Visit Page