1

簡単な質問があります。現在、サーバーへのデータ送信に関連するアプリの特定の部分を書いています。

テキストを送信しようとしましたが、成功しました。画像を送信しました。成功しました。私が今やろうとしているのは、1 つの POST リクエスト内で両方を送信することです。マルチパート/フォームデータと境界と呼ばれるものを使用する必要があることがわかりましたが、それに関する情報はもう見つかりませんでした。

では、1 つの単純な POST 要求でテキストと画像の両方を送信するにはどうすればよいでしょうか? また、アップロード中やその後などにエラーを確認するにはどうすればよいですか?

ありがとう!

私が書いた参照コードですが、0バイトの情報を送信しています:

NSData *imageData = UIImageJPEGRepresentation(sendImage, 1.0);
    // setting up the request object now
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:@"http://posttestserver.com/post.php?dir=something"]];
    [request setHTTPMethod:@"POST"];
    NSString *boundary = @"---------------------------54737809831466490885746641449";
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];

    NSMutableData *body = [NSMutableData data];
    [body appendData:[[NSString stringWithFormat:@"rn--%@rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\"reportingImage.jpg\"rn" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Type: application/octet-streamrnrn" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:imageData]];
    [body appendData:[[NSString stringWithFormat:@"rn--%@--rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
     [body appendData:[@"Content-Type: text/xml" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[[alertView textFieldAtIndex:0] text] dataUsingEncoding:NSUTF8StringEncoding]];
    // setting the body of the post to the reqeust
    [request setHTTPBody:body];

    // now lets make the connection to the web
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

    NSLog(returnString);
4

2 に答える 2

1

これは、テキストと画像を送信する実用的なスニペットです。オプションで、それぞれにいくつかのパラメーターを持ついくつかのテキストを送信します

//After dismissing the alert, we get its text (user location and notes) and the picture he took

     NSMutableData *body = [NSMutableData data];
     NSURL *url = [NSURL URLWithString:@"http://posttestserver.com/post.php?dir=Doda"]; //Test server, you can access it online to see the upload
     NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
     [req setHTTPMethod:@"POST"];
     NSString *boundary = @"---------------------------14737809831466499882746641449"; //I have no idea what this is, but without it the code won't work
     NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
     [req setValue:contentType forHTTPHeaderField: @"Content-Type"];

     //Attaching image
     [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
     [body appendData:[@"Content-Disposition: attachment; name=\"imageOfReport\"; filename=\"imageOfReport.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
     [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
     [body appendData:[NSData dataWithData:UIImageJPEGRepresentation(sendImage, 1.0)]]; //Crucial, getting a JPEG version of the image and sending it
     [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

     [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
     [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"report_description\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
     [body appendData:[[[alertView textFieldAtIndex:0] text] dataUsingEncoding:NSUTF8StringEncoding]]; //Crucial, taking the text from the Alert and sending it
     [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
     [req setHTTPBody:body];

     //Below are few lines which can add other parameters and text
     /*        [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
     [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"spid\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
     [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
     [req setHTTPBody:body];*/

    NSURLConnection *sendingTheData2 = [[NSURLConnection alloc] initWithRequest:req delegate:self startImmediately:YES]; //Sent! ;)
于 2013-04-12T12:46:43.027 に答える
0

おそらくAFNetworkingのようなネットワーク ライブラリを使用して、時間を節約したいと思うでしょう :)

于 2013-04-12T11:54:39.440 に答える