使用 SOAP 使用 WCF 服务将图像从 iPhone 上传到服务器 [英] Upload image from iPhone to server using WCF service using SOAP
问题描述
我正在为我的应用程序使用 wcf SOAP 服务,我发送如下请求.
hiii, i'm using wcf SOAP services for my app and i send the request like below.
postStr = [NSString stringWithFormat:@"<?xml version="1.0"?>
"
"<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
"
"<s:Body>
"
"<InsUpdDelActivityInfo xmlns="http://tempuri.org/">
"
"<objEventsContent xmlns:d4p1="http://schemas.datacontract.org/2004/07/iCampuslite.Model.ActivityStream" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
"
"<d4p1:ActCommentId i:nil="true" />
"
"<d4p1:ActSubTypeCd i:nil="true" />
"
"<d4p1:ActType>Status</d4p1:ActType>
"
"<d4p1:ActTypeCd>1</d4p1:ActTypeCd>
"
"<d4p1:ActivityAnswersList />
"
"<d4p1:ActivityComments />
"
"<d4p1:ActivityId i:nil="true" />
"
"<d4p1:ActivityLike />
"
"<d4p1:ActivityName>%@</d4p1:ActivityName>
"
"<d4p1:ActivityStreamImagesBytes>%@</d4p1:ActivityStreamImagesBytes>
"
"<d4p1:AnswerDesc i:nil="true" />
"
"<d4p1:AnswerId>0</d4p1:AnswerId>
"
"<d4p1:CommentDesc i:nil="true" />
"
"<d4p1:CommentId i:nil="true" />
"
"<d4p1:CreatedUserId>%@</d4p1:CreatedUserId>
"
"<d4p1:CreatedUserName i:nil="true" />
"
"<d4p1:FileOrLinkName i:nil="true" />
"
"<d4p1:IsLiked>0</d4p1:IsLiked>
"
"<d4p1:IsTotalSchool i:nil="true" />
"
"<d4p1:LikeCount>0</d4p1:LikeCount>
"
"<d4p1:LinkImage i:nil="true" />
"
"<d4p1:ObjTypeCdId i:nil="true" />
"
"<d4p1:ObjTypeId i:nil="true" />
"
"<d4p1:OperationMode>I</d4p1:OperationMode>
"
"<d4p1:OperationType i:nil="true" />
"
"<d4p1:OperationTypeId i:nil="true" />
"
"<d4p1:OrganizationId>%@</d4p1:OrganizationId>
"
"<d4p1:OtherActivityId i:nil="true" />
"
"%@
"
"</objEventsContent>
"
"<ismobile>true</ismobile>
"
"</InsUpdDelActivityInfo>
"
"</s:Body>
"
"</s:Envelope>", statusText, [appDelegateObj.loginUserInfoDict valueForKey:@"a:UserId"], [appDelegateObj.loginUserInfoDict valueForKey:@"a:OrgId"], workspaceStr];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@iCampusliteMobileService/ActivityStreamSl.svc", appDelegateObj.baseURL]]];
NSString *messageLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postStr length]];
[request addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"http://tempuri.org/IActivityStreamSl/InsUpdDelActivityInfo" forHTTPHeaderField:@"SOAPAction"];
[request addValue:messageLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[postStr dataUsingEncoding:NSUTF8StringEncoding]];
NSError *error = nil;
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
在上面的请求中有一个元素 ActivityStreamImagesBytes
这是我必须传递图像的 base64binary
参数.
in the above request there is an element ActivityStreamImagesBytes
which is base64binary
parameter i have to pass the image.
我尝试过使用许多不同的格式.
I've tried using many different formats.
这是 wcf 服务截图
here is wcf service screenshot
这里是服务器端代码
public string byteArrayToImage(byte[] byteArrayIn,string fileName)
{
if (byteArrayIn != null)
{
ActivityStreamSl.LogMsg("Byte Array Count : "+byteArrayIn.Length.ToString(), "D:\log.txt");
var serverfile = "D:somepathsomepathsomefolder";
var getfile = HelperClass.Filesavehelper(Constants.UploadPaths.ActivityStream, "testfilename.png", serverfile);
FileStream file = new FileStream(getfile, FileMode.Create);
file.Write(byteArrayIn, 0, byteArrayIn.Length);
file.Close();
file.Dispose();
return getfile;
}
else
{
ActivityStreamSl.LogMsg("Byte array is null","D:\log.txt");
}
return "";
}
服务器需要一个字节数组,但我不知道如何发送它?我不知道 base64binary 数据类型是什么?我是否必须从 NSData *data = UIImageJPEGRepresentation([UIImage imageNamed:@"popular.png"], 0.7);
the server is expecting a byte array and i dont know how to send it?
And i dont know what a base64binary data type is?
Do I have to send the base64 encoded string or byte array or just data of image from NSData *data = UIImageJPEGRepresentation([UIImage imageNamed:@"popular.png"], 0.7);
任何帮助都是可观的
推荐答案
Hiii,我有我的问题
Hiii, I've got what my problem is
在服务器端,它需要一个字节数组,
On server side, its expecting a byte array,
所以,我必须按如下方式发送我的请求.
So, I've to send my request as follows.
将base64字符串转成字节数组
Converting the base64 string into byte array
postStr = [NSString stringWithFormat:@"<?xml version="1.0"?>
"
"<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
"
"<s:Body>
"
"<InsUpdDelActivityInfo xmlns="http://tempuri.org/">
"
"<objEventsContent xmlns:d4p1="http://schemas.datacontract.org/2004/07/iCampuslite.Model.ActivityStream" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
"
"<d4p1:ActCommentId i:nil="true" />
"
"<d4p1:ActSubTypeCd i:nil="true" />
"
"<d4p1:ActType>Status</d4p1:ActType>
"
"<d4p1:ActTypeCd>1</d4p1:ActTypeCd>
"
"<d4p1:ActivityAnswersList />
"
"<d4p1:ActivityComments />
"
"<d4p1:ActivityId i:nil="true" />
"
"<d4p1:ActivityLike />
"
"<d4p1:ActivityName>%@</d4p1:ActivityName>
"
"<d4p1:ActivityStreamImagesBytes dt: dt:"Base64Binary"><dt:length>%d</dt:length><dt:byte>%@</dt:byte>......................................</d4p1:ActivityStreamImagesBytes>
"
"<d4p1:AnswerDesc i:nil="true" />
"
"<d4p1:AnswerId>0</d4p1:AnswerId>
"
"<d4p1:CommentDesc i:nil="true" />
"
"<d4p1:CommentId i:nil="true" />
"
"<d4p1:CreatedUserId>%@</d4p1:CreatedUserId>
"
"<d4p1:CreatedUserName i:nil="true" />
"
"<d4p1:FileOrLinkName i:nil="true" />
"
"<d4p1:IsLiked>0</d4p1:IsLiked>
"
"<d4p1:IsTotalSchool i:nil="true" />
"
"<d4p1:LikeCount>0</d4p1:LikeCount>
"
"<d4p1:LinkImage i:nil="true" />
"
"<d4p1:ObjTypeCdId i:nil="true" />
"
"<d4p1:ObjTypeId i:nil="true" />
"
"<d4p1:OperationMode>I</d4p1:OperationMode>
"
"<d4p1:OperationType i:nil="true" />
"
"<d4p1:OperationTypeId i:nil="true" />
"
"<d4p1:OrganizationId>%@</d4p1:OrganizationId>
"
"<d4p1:OtherActivityId i:nil="true" />
"
"%@
"
"</objEventsContent>
"
"<ismobile>true</ismobile>
"
"</InsUpdDelActivityInfo>
"
"</s:Body>
"
"</s:Envelope>", statusText, [byteArray length], byteArray[0], byteArray[1], .................................., [appDelegateObj.loginUserInfoDict valueForKey:@"a:UserId"], [appDelegateObj.loginUserInfoDict valueForKey:@"a:OrgId"], workspaceStr];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@blah....blah....blah....", appDelegateObj.baseURL]]];
NSString *messageLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postStr length]];
[request addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"http://tempuri.org/blah......blah.....blah................" forHTTPHeaderField:@"SOAPAction"];
[request addValue:messageLength forHTTPHeaderField:@"Content-Length"];
[请求 setHTTPMethod:@"POST"];
[请求 setHTTPBody:[postStr dataUsingEncoding:NSUTF8StringEncoding]];
NSError *error = nil;
NSData *returnData = [NSURLConnection sendSynchronousRequest:请求返回响应:nil 错误:&error];
我托管了这些服务,并且看到了请求的 xml 格式.无论如何,谢谢@Duraiamuthan.H
I've hosted the services and there i've seen the xml format of the request. Any way thank you @Duraiamuthan.H
这篇关于使用 SOAP 使用 WCF 服务将图像从 iPhone 上传到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!