我有这样的表格:
<form id="form" action="/post" method="POST" enctype="multipart/form-data">
<input type="file" name="image" />
<input type="text" name="name1" />
<input type="text" name="name2" />
<textarea name="text"></textarea>
<input type="text" name="name3" />
</form>
我有这样的方法:
- (void)sendFields {
UIImage *tempImage = imageViewToPost.image;
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setHTTPMethod:@"POST"];
[request setURL:[NSURL URLWithString:@"http://url/post/"]];
NSString *boundary = @"------------0xKhTmLbOuNdArY";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n%@\r\n",boundary] dataUsingEncoding:NSASCIIStringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"image\"; filename=\"photo.png\"\r\n" dataUsingEncoding:NSASCIIStringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSASCIIStringEncoding]];
[body appendData:[NSData dataWithData:UIImageJPEGRepresentation(tempImage, 90)]];
[body appendData:[[NSString stringWithFormat:@"\r\n%@\r\n",boundary] dataUsingEncoding:NSASCIIStringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"name1\"\r\n\r\n" dataUsingEncoding:NSASCIIStringEncoding]];
[body appendData:[nameToPost dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"name2\"\r\n\r\n" dataUsingEncoding:NSASCIIStringEncoding]];
[body appendData:[priceToPost dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"text\"\r\n\r\n" dataUsingEncoding:NSASCIIStringEncoding]];
[body appendData:[commentToPost dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"name3\"\r\n\r\n" dataUsingEncoding:NSASCIIStringEncoding]];
[body appendData:[coordsToPost dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSASCIIStringEncoding]];
[request setHTTPBody:body];
NSError *error;
NSURLResponse *response;
NSData *result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *aStr = [[[NSString alloc] initWithData:result encoding:NSASCIIStringEncoding] autorelease];
NSLog(@"Result: %@", aStr);
[request release];
}
它不起作用。这里有什么问题?
多谢你们。