我正在尝试上传图片以及一些表单字段。我可以使用以下链接作为参考将图像上传到服务器。
File Upload to HTTP server in iphone programming
但是如果我尝试发送一些表单字段和图像我是错误响应。这是我正在使用的代码
-(void)saveData{
NSString *urlString = @"Sample url";
NSString *filename = @"filename";
NSMutableURLRequest *request= [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[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--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[self generateDataFromText:FORM DATA IN JSON fieldName:@"add_product"]];
[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"files\"; filename=\"%@.jpg\"\r\n", filename] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[NSData dataWithData:fileData]];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postbody];
[request setValue:APIKEY forHTTPHeaderField:@"X-API-KEY"];
NSURLResponse *urlResp;
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResp error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSHTTPURLResponse *resp = (NSHTTPURLResponse *) urlResp;
NSLog(@"status code: %ld, response string: %@",(long)[resp statusCode],returnString);
}
-(NSMutableData *)generateDataFromText:(NSString *)dataText fieldName:(NSString *)fieldName
{
NSString *post = [NSString stringWithFormat:@"--AaB03x\r\nContent-Disposition: form-data; name=\"%@\"\r\n\r\n", fieldName];
// Get the post header int ASCII format:
NSData *postHeaderData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
// Generate the mutable data variable:
NSMutableData *postData = [[NSMutableData alloc] initWithLength:[postHeaderData length]];
[postData setData:postHeaderData];
NSData *uploadData = [dataText dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
// Add the text:
[postData appendData: uploadData];
// Add the closing boundry:
[postData appendData: [@"\r\n" dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]];
// Return the post data:
return postData;
}
所以有人请帮我用POST方法将表单数据和am image一起发送到PHP服务器。提前致谢。
答案 0 :(得分:0)
每个字段应位于其自己的边界@“\ r \ n - %@ \ r \ n”。因此,请进行以下更改以使代码正常工作。
删除该行:
[postbody appendData:[self generateDataFromText:FORM DATA IN JSON fieldName:@"add_product"]];
行后
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
输入以下代码:
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; add_product=\"%@\"\r\n\r\n",key] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[self generateDataFromText:FORM DATA IN JSON] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithString:@"\r\n--%@\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
对于每个字段,您需要添加这4行。
您应该考虑使用AFNetworking。它很容易上班。
使用AFNetworking非常容易。通过Cocoapods将AFNetworking添加到您的项目中,以下是说明: https://github.com/AFNetworking/AFNetworking
设置AFNetworking后,请使用以下代码:
NSString *apiName = URL;
UIImage *yourImage;
NSData *imgData = UIImageJPEGRepresentation(yourImage,1.0);
imgData = [imgData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];];
NSData *otherFields = [self generateDataFromText:FORM DATA IN JSON];
NSDictionary * params = [[NSDictionary alloc] initWithObjectsAndKeys:imgData, @"imgFieldName",otherFields,@"add_product",nil];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:apiName parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
//posted successfully
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//failed
}];
这是一个非常简单的Post方法,其图像采用Base64编码。您可以根据需要在params字典中添加任意数量的字段。