I’m developing an application in C# that connects to Appcelerator Cloud Service, so far I can make queries and create custom objects, now the problem is when I try to create a photo in ACS. I looked at this link and modified my code like this:
Image img = pbPhoto.Image;
img.Save(Application.StartupPath + "\\tmp.jpg", System.Drawing.Imaging.ImageFormat.Jpeg); //saving the image temporally in hard drive
url = "https://api.cloud.appcelerator.com/v1/photos/create.json?key=appkey&_session_id=" + session;
HttpWebRequest wrGetUrl = (HttpWebRequest)WebRequest.Create(url);
String boundary = "B0unD-Ary";
wrGetUrl.ContentType = "multipart/form-data; boundary=" + boundary;
wrGetUrl.Method = "POST";
String postData = "--" + boundary + "\nContent-Disposition: form-data\n\n";;
postData += "\n--" + boundary + "\nContent-Disposition: form-data; name=\"file\" filename=\"" + Application.StartupPath + "\\tmp.jpg" + "\"\nContent-Type: image/jpeg\n\n";
byteArray = Encoding.UTF8.GetBytes(postData);
byte[] filedata = null;
using (BinaryReader readerr = new BinaryReader(File.OpenRead(Application.StartupPath + "\\tmp.jpg")))
filedata = readerr.ReadBytes((int)readerr.BaseStream.Length);
wrGetUrl.ContentLength = byteArray.Length + filedata.Length;
wrGetUrl.GetRequestStream().Write(byteArray, 0, byteArray.Length);
wrGetUrl.GetRequestStream().Write(filedata, 0, filedata.Length);
objStream = wrGetUrl.GetResponse().GetResponseStream();
reader = new StreamReader(objStream);
I tried this but I got the following error
The remote server returned an error: (500) Internal Server Error.
I checked my ACS log but the request didn’t show up (guess because it was a 500 error). What should I change in my code to upload the photo and crete the photo in ACS? Thanks for any help you may give.
Found the solution for this problem:
EDIT: I changed the way I wrote the headers into the RequestStream, the way I was writing it wasn’t the proper one to send a picture to Appcelerator Cloud Service, by sending requests through curl and checking the Log in ACS I was able to come up with the right headers.
Hope this may help anyone with similar problems.