Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
474 views
in Technique[技术] by (71.8m points)

image - iPhone UIImage upload to web service

I worked on this for several hours today and I'm pretty close to a solution but clearly need some help from someone who's pulled this off. I'm trying to post an image to a web service from the iPhone. I'll post the code first then explain everything I've tried:

NSData *imageData = UIImageJPEGRepresentation(barCodePic, .9);


NSString *soapMsg = 
[NSString stringWithFormat:
 @"<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><WriteImage xmlns="http://myserver/imagewebservice/"><ImgIn>%@</ImgIn></WriteImage></soap:Body></soap:Envelope>",  [NSData dataWithData:imageData]
 ];


NSURL *url = [NSURL URLWithString:@"http://myserver/imagewebservice/service1.asmx"];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];

NSString *msgLength = 
[NSString stringWithFormat:@"%d", [soapMsg length]];
[req addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[req addValue:@"http://myserver/imagewebservice/WriteImage" forHTTPHeaderField:@"SOAPAction"];
[req addValue:msgLength forHTTPHeaderField:@"Content-Length"];
[req setHTTPMethod:@"POST"];
[req setHTTPBody: [soapMsg dataUsingEncoding:NSUTF8StringEncoding]];

conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
if (conn) {
    webData = [[NSMutableData data] retain];
}    

First thing, this code works fine for anything but an image. The web service is running on my local network and I can change the source code at will and if I change the "ImgIn" parameter to a string and pass a string in, everything works fine, I get a return value no problem. So there are no connectivity issues at all, I'm able to call and get data from this web service on this server no problems. But I need to upload an image to this web service via the ImgIn parameter, so the above code is my best shot so far. I also have didReceiveResponse, didReceiveData, didFailWithError, etc all being handled. The above code fires off didRecieveResponse every time. However didReceiveData is never fired and it's like the web service itself never even runs. When I debug the web service itself, it runs and debugs fine when I use a string parameter, but with the image parameter, it never even debugs when I call it. It's almost like the ImgIn parameter is too long (it's huge when I output it to the screen) and the web service just chokes on it. I've read about having to encode to Base64 when using this method, but I can't find any good links on how that's done. If that's what I'm doing wrong, can you PLEASE provide code as to how to do this, not just "you need to use Base64", I'd really appreciate it as I can find almost nothing on how to implement this with an example. Other than that, I'm kind of lost, it seems like I'm doing everything else right. Please help!

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It fails because the %@ format specifier takes an object and calls -(NSString*)description to turn it into a string. -[NSData description] returns something like @"<31 32 33 65 66 67>" for (ASCII) "123abc", hence you're submitting <31 32 33 65 66 67> which obviously isn't valid XML. Don't glue strings together (it'll be vulnerable to XML injection, among other things).

I would drop SOAP and just use

[req addValue:@"image/jpeg" forHTTPHeaderField:@"Content-Type"];
[req setHTTPBody:imgData]

If you want to use SOAP, then there's probably an Objective-C SOAP library which will do packaging for you. Failing that, you can manually specify xs:base64Binary (I think) and use base64-encoded data. Neither of these is ideal, since it makes the data 33% larger. If you're unlucky, a SOAP library will make it 20000% larger!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...