2010-04-23 66 views
2

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

這並不工作,但我不知道爲什麼。你能幫我麼?!!我究竟做錯了什麼?

+0

你得到了什麼錯誤? – 2010-04-23 14:22:23

+1

您是否嘗試過使用像Charles這樣的Web調試代理,查看發送給服務器的內容以及正在發送的內容? – Twelve47 2010-04-23 14:27:27

回答

0

您可以設置文件名作爲photo.png但使用JPEG數據。可能是這個問題?

0
<?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!」

和NO:我改變了文件名photo.jpg - 沒有改變...

我監視的請求,得到了下面的帖子:

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

它看起來像你的請求缺少終止邊界標記。根據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];  
} 
+0

根據RFC 2046,多部分MIME消息的每個部分都需要在邊界標記之前。最後一部分需要用最後的邊界標記來關閉。最後的邊界標記附有「 - 」以表示沒有任何部分跟隨。謝謝!我已經能夠發送我的圖像與鍵值對一起感謝你!它的凌晨3點我可以去睡覺:) – 2012-05-20 00:47:54