将图像数据(NSData)发送到服务器 [英] Sending an image data(NSData) to the server
问题描述
我想将图像数据发送到服务器,为此,我已将该数据附加到请求的HTTP主体并发送它。我使用以下代码。
I want to send image data to the server, For that, I have appended that data to the HTTP body of the request and sent it. I am using the following code.
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:@"http://developers.our-works.com/forms/TestReceiver.aspx"]];
[request setHTTPMethod:@"POST"];
NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
// now lets create the body of the post
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@\"\r\n",tempFileName] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imgdata]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//data into the string..... verifying............
NSString *strTest = [[NSString alloc] initWithData:body encoding:NSUTF8StringEncoding];
NSLog(@"%@",strTest);
// setting the body of the post to the request
[request setHTTPBody:body];
// now lets make the connection to the web
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
现在我必须发送两个不同图像的NSData。所以,只想问一下,我可以在同一个体内发送这两个不同的图像的NSData。
或者,我必须为其他图像的数据编写相同的代码。
Now I have to send the NSData of the two different images. So, just want to ask that, can I send those two different image's NSData within the same body as above. Or, I have to code the same thing for the other image's Data.
请帮助我解决这个问题。谢谢你。
Please, help me out of this. Thanking you.
推荐答案
如果服务器需要两个图像,你必须为其他数据编码相同的东西
这取决于web service.you可以从以下链接中获取想法:
$
http://urenjoy.blogspot.com/2009/12/pass-parameter-web-service-image-upload.html
You have to code same thing for other data if server needs two images together
It depends on web service.you can take idea from following link:
http://urenjoy.blogspot.com/2009/12/pass-parameter-web-service-image-upload.html
这篇关于将图像数据(NSData)发送到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!