C# Can';t在C中使用HttpWebRequest将照片上载到Appcelerator云服务#
我正在用C#开发一个连接到Appcelerator云服务的应用程序,到目前为止我可以进行查询并创建自定义对象,现在的问题是当我尝试在ACS中创建照片时。我查看了链接并修改了代码,如下所示:C# Can';t在C中使用HttpWebRequest将照片上载到Appcelerator云服务#,c#,upload,httpwebrequest,cloud,appcelerator,C#,Upload,Httpwebrequest,Cloud,Appcelerator,我正在用C#开发一个连接到Appcelerator云服务的应用程序,到目前为止我可以进行查询并创建自定义对象,现在的问题是当我尝试在ACS中创建照片时。我查看了链接并修改了代码,如下所示: Image img = pbPhoto.Image; img.Save(Application.StartupPath + "\\tmp.jpg", System.Drawing.Imaging.ImageFormat.Jpeg); //saving the image temporally in hard
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);
我尝试了这个,但我得到了以下错误
远程服务器返回错误:(500)内部服务器错误
我检查了我的ACS日志,但请求没有显示(猜测是因为它是一个500错误)。我应该在我的代码中更改什么来上传照片并将照片保存在ACS中?感谢您提供的帮助。找到了此问题的解决方案:
byte[] filedata = null;
using (BinaryReader readerr = new BinaryReader(File.OpenRead(pathToImage)))
filedata = readerr.ReadBytes((int)readerr.BaseStream.Length);
string boundary = "-_1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
Stream stream = request.GetRequestStream();
request.ContentType = string.Format("multipart/form-data; boundary={0}", boundary);
StreamWriter writer = new StreamWriter(stream);
writer.Write("--");
writer.WriteLine(boundary);
writer.WriteLine(@"Content-Disposition: form-data; name=""{0}""; filename=""{1}""", "your_name", "your_photo_file_name");
writer.WriteLine(@"Content-Type: application/octet-stream");
writer.WriteLine(@"Content-Length: " + filedata .Length);
writer.WriteLine();
writer.Flush();
Stream output = writer.BaseStream;
output.Write(filedata , 0, filedata .Length);
output.Flush();
writer.WriteLine();
writer.Write("--");
writer.Write(boundary);
writer.WriteLine("--");
writer.Flush();
编辑:我改变了将标题写入RequestStream的方式,我的写入方式将图片发送到Appcelerator云服务是不合适的,通过curl发送请求并检查ACS中的日志,我能够找到正确的标题
希望这可以帮助任何有类似问题的人。到底是什么问题?您没有指定您更改了什么以使其工作。我无法将图片上载到ACS,我添加了详细信息,说明当我上载带有curl的照片时,我是如何发现ACS收到了哪种标题的。