我有以下HTML表格:iPhone開發:使用POST提交表單
<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>
而下面的iPhone SDK發佈方法:
- (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];
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];
}
這並不工作,但我不知道爲什麼。你能幫我麼?!!我究竟做錯了什麼?
你得到了什麼錯誤? – 2010-04-23 14:22:23
您是否嘗試過使用像Charles這樣的Web調試代理,查看發送給服務器的內容以及正在發送的內容? – Twelve47 2010-04-23 14:27:27