我正在尝试使用Unity和Web服务发送图像(和其他字段)。
这是我的Unity c#客户端代码:
byte[] photoBytes = ImageConversion.EncodeToJPG( myObject.GetPhotoTexture(), 90);
WWWForm form2= new WWWForm();
form2.AddField("field1", field1);
form2.AddField("field2", field2);
if (photoBytes != null)
{
form2.AddBinaryData("photoBytes", photoBytes,FrameID.ToString() + ".jpg");
}
string url = gsStaticClassManager.WEBSERVICE_URL + "UploadJustPhoto";
WWW www = new WWW(url, form2);
yield return www;
if(!string.IsNullOrEmpty(www.error))
Debug.Log(www.error);
这是我的Web服务
[WebMethod]
public string UploadJustPhoto(string guid, int frameID, byte[] photoBytes)
{
try
{
if (photoBytes != null)
{
string completeFileName = frameID + ".jpg";
MemoryStream ms = new MemoryStream(photoBytes);
FileStream fs = new FileStream(completeFileName, FileMode.Create);
ms.WriteTo(fs);
// clean up
ms.Close();
fs.Close();
fs.Dispose();
return "OK";
}
}
catch (Exception ex)
{
return "KO";
}
return "KO";
}
我总是遇到“ HTTP 500错误”。
可能是什么原因?
谢谢
编辑:我发现了以下特定错误:
System.InvalidOperationException: Request format is invalid: multipart/form-data; boundary="ANX8OOvdTQP97L1wHRXlndHJH9pnuLM8cWVzv13j".
at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()