2013-04-12 72 views
1

我有一個簡單的問題 - 我目前正在寫一個特定的部分在我的應用程序有關發送數據到服務器。發送UIImage和文本到服務器

我試着發送文本,成功了。發送圖像,成功。 我現在想要做的是在一個POST請求中發送它們。 我發現我需要使用稱爲multipart/form-data和邊界的東西,但我還沒有找到有關它的信息。

那麼如何在一個簡單的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); 
+0

幾個PARAMS試試這個http://stackoverflow.com/questions/14598044/how-to-send-image-and-text-toget她使用nsmutableurlrequest – Buntylm

+0

在該答案的代碼工作,非常感謝! –

+0

我也給出了同樣的答案。 – Buntylm

回答

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! ;)