如何在iPhone SDK中将照片上传到服务器?

时间:2014-07-07 10:40:40

标签: ios asp.net iphone web-services

以下是将照片上传到服务器的方法。有两个参数:DeviceTokenContentModel.ContentModel,其中包含字典。在2个字段中,有AlbumIDContentPath

NSURL *rtfUrl = [[NSBundle mainBundle] URLForResource:@"AlertBody" withExtension:@".png"];
NSString *deviceToken = @"5FF2C5A6-3930-4102-99A7-A55107B4375C";
NSString *loStr = [NSString stringWithFormat:myUrl];

loStr = [loStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:loStr]];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];

NSMutableData *postbody = [NSMutableData data];

[postbody appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n",@"deviceToken"] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithString:deviceToken] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

NSDictionary *contentModel = @{
                            @"AlbumID" : @"0",
                            @"contentPath" : [rtfUrl absoluteString],
                            };
[postbody appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n",@"contentModel"] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"%@",contentModel] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

//Adding Image

NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"AlertBody.png"], 0.3);
[postbody appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
 [postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"myfile\"; filename=\"%@\"\r\n",@"AlertBody.png"] dataUsingEncoding:NSUTF8StringEncoding]]; // filetype=\"image/png\";
[postbody appendData:[@"Content-Type: image/png\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[NSData dataWithData:imageData]];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];


//Close form

[postbody appendData:[[NSString stringWithFormat:@"--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];



//send data to server

[request setHTTPBody:postbody];

NSString *msgLength = [NSString stringWithFormat:@"%d", [postbody length]];
[request addValue: msgLength forHTTPHeaderField:@"Content-Length"];
NSURLResponse *response = NULL;
NSError *requestError = NULL;
NSData *responseData1 = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&requestError];

NSString *responseString = [[NSString alloc] initWithData:responseData1 encoding:NSUTF8StringEncoding];

NSLog(@"response string ==%@;",responseString);

这是我的代码。这有什么错误?我得到了这样的答复:Source code **500** .web service is created using ASP.net.

请帮帮我。我经常遇到这个问题。

1 个答案:

答案 0 :(得分:0)

你能试试下面的代码吗?它经过测试并且运行良好。希望它也可以帮助您解决问题。

NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];

[request addValue:contentType forHTTPHeaderField: @"Content-Type"]; 

[postData appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

[postData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"yourImage.jpg\"\r\n", @"file"] dataUsingEncoding:NSUTF8StringEncoding]];

[postData appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

[postData appendData:imageData];

[postData appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

[postData appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];