【问题标题】:iPhone Development: Use POST to submit a formiPhone开发:使用POST提交表单
【发布时间】:2011-02-11 13:23:49
【问题描述】:

我有以下 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];    
}

这不起作用,但我不知道为什么。你能帮我么?!!我做错了什么?

【问题讨论】:

  • 你遇到了什么错误?
  • 您是否尝试过使用像 Charles 这样的 Web 调试代理来查看发送到服务器的内容以及发回的内容?

标签: iphone objective-c forms post upload


【解决方案1】:

您的请求似乎缺少终止边界标记。根据 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];    
}

【讨论】:

  • 根据 RFC 2046,多部分 mime 消息的每个部分前面都需要一个边界标记。最后一部分需要用最终的边界标记关闭。最后的边界标记附加了“--”,表示后面没有任何部分。谢谢!感谢您,我已经能够将我的图像与键值对一起发送!凌晨 3 点,我可以睡觉了 :)
【解决方案2】:
<?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!");

?>

这是 PHP 代码,我总是得到“nicht erfolgreich!”

不:我将文件名更改为 photo.jpg - 没有更改...

我监控了请求并得到了以下 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

ˇÿˇ‡

【讨论】:

    【解决方案3】:

    您将文件名设置为photo.png,但使用的是JPEG 数据。这可能有问题吗?

    【讨论】:

      【解决方案4】:

      下载http://www.charlesproxy.com/

      尝试从浏览器和 iPhone 模拟器执行相同的 POST 操作,看看有什么不同。

      【讨论】:

        猜你喜欢
        • 2011-12-26
        • 1970-01-01
        • 1970-01-01
        • 2014-01-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多