iPhone Development: Use POST to submit a form
I've got the following html form:
<form method="post" action="http://shk.ecomd.de/up.php" enctype="multipart/form-data">
<input type="hidden" name="id" value="12345" />
<input type="file" name="pic" />
<input type="submit" />
</form>
And the following iPhone SDK Submit method:
- (void)sendfile {
UIImage *tempImage = [UIImage imageNamed:@"image.jpg"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setHTTPMethod:@"POST"];
[request setURL:[NSURL URLWithString:@"http://url..../form.php"]];
NSString *boundary = @"------------0xKhTmLbOuNdArY";
NSString *contentType = [NSStr开发者_StackOverflowing 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=\"id\"\r\n\r\n" dataUsingEncoding:NSASCIIStringEncoding]];
[body appendData:[@"12345" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n%@\r\n",boundary] dataUsingEncoding:NSASCIIStringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"pic\"; 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%@",boundary] dataUsingEncoding:NSASCIIStringEncoding]];
// setting the body of the post to the reqeust
[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];
}
This does not work, but I have no clue why. Can you please help me?!! What am I doing wrong?
It looks like your request is missing the terminating boundary marker. Per RFC 2046, each part of a multi-part mime message needs to be preceded by a boundary marker. The last part needs to be closed with a final boundary marker. The final boundary marker has "--" appended to it to indicate that no parts follow. Try this code:
- (void)sendfile {
UIImage *tempImage = [UIImage imageNamed:@"image.jpg"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setHTTPMethod:@"POST"];
[request setURL:[NSURL URLWithString:@"http://url..../form.php"]];
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=\"id\"\r\n\r\n" dataUsingEncoding:NSASCIIStringEncoding]];
[body appendData:[@"12345" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n%@\r\n",boundary] dataUsingEncoding:NSASCIIStringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"pic\"; 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%@",boundary] dataUsingEncoding:NSASCIIStringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
//add terminating boundary marker
[body appendData:[[NSString stringWithFormat:@"\r\n%@--",boundary] dataUsingEncoding:NSASCIIStringEncoding]];
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];
}
Download http://www.charlesproxy.com/
Try to perform the same POST action from your browser and from the iPhone simulator and see what the differences are.
You set filename as photo.png
but using JPEG data. May be problem in this?
<?php
require_once '__config.php';
if ($_FILES) {
if ($_FILES['pic']['type']=="image/jpeg" && intval($_POST['id'])>0) {
copy($_FILES['pic']['tmp_name'],$_SERVER['DOCUMENT_ROOT'].'/u/'.intval($_POST['id']).'.jpg');
$query="update tleistung set bild=1 where id=".intval($_POST['id']);
$res=$db->exec($query);
die('/u/'.intval($_POST['id']).'.jpg');
}
}
die("nicht erfolgreich!");
?>
This is the PHP-Code and I always get "nicht erfolgreich!"
And no: I changed the filename to photo.jpg - no change...
I monitored the Request and got the following POST:
POST /up.php HTTP/1.1
Host: url...
User-Agent: iDeXs/0.97 CFNetwork/445.6 Darwin/10.3.0
Content-Type: multipart/form-data; boundary=------------0xKhTmLbOuNdArY
Referer: http://url.../up.php
Content-Length: 380606
Accept: */*
Accept-Language: de-de
Accept-Encoding: gzip, deflate
Connection: keep-alive
------------0xKhTmLbOuNdArY
Content-Disposition: form-data; name="id"
12345
------------0xKhTmLbOuNdArY
Content-Disposition: form-data; name="pic"; filename="photo.jpg"
Content-Type: image/jpeg
ˇÿˇ‡
精彩评论