作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下 html 表单:
<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];
}
这不起作用,但我不知道为什么。你能帮我么?!!我做错了什么?
最佳答案
您的请求似乎缺少终止边界标记。根据 RFC 2046,多部分 mime 消息的每个部分前面都需要有边界标记。最后一部分需要用最终边界标记来封闭。最终的边界标记后面附加了“--”,表示后面没有任何部分。试试这个代码:
- (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];
}
关于iPhone开发: Use POST to submit a form,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2699259/
我是一名优秀的程序员,十分优秀!