2012-06-29 18 views
10

Jak mogę przesłać niektóre informacje tekstowe (ciąg tekstowy) i plik obrazu za pomocą tego samego żądania http post na serwer. Przesyłam zdjęcia same, ale nie mogę uzyskać z tego tekstu. Dzięki!Jak wysłać dane pocztowe i plik obrazu do serwera Xcode

+0

Która usługa to jest? –

+0

Cel c, To jest podobne pytanie, ale potrzebuję tekstu. – sebi

+0

http://stackoverflow.com/questions/125306/how-can-i-upload-a-photo-to-a-server--the-iphone – sebi

Odpowiedz

18

użyć tego kodu, aby przesłać obraz i textLabel

NSData *imageData = UIImageJPEGRepresentation("yourImage",0.2);  //change Image to NSData 

if (imageData != nil) 
{ 
    filenames = [NSString stringWithFormat:@"TextLabel"];  //set name here 
     NSLog(@"%@", filenames); 
    NSString *urlString = @"http://xxxxxxx/yyyyy.php"; 

    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 
    [request setURL:[NSURL URLWithString:urlString]]; 
     [request setHTTPMethod:@"POST"]; 

     NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"]; 
     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:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"filenames\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[filenames dataUsingEncoding:NSUTF8StringEncoding]]; 

    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];  
    [body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userfile\"; filename=\".jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 

     [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[NSData dataWithData:imageData]]; 
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] 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); 
    NSLog(@"finish"); 
} 

W użyciu bocznej php ten kod

$myparam = $_POST['userfile'];  //getting image Here 
$mytextLabel= $_POST['filenames'] //getting textLabe Here 
echo $myparam; 
echo $mytextLabel;  
$target_path = "uploads/"; 
$target_path = $target_path . basename($_FILES['myfile']['name']);   

if(move_uploaded_file($_FILES['myfile']['tmp_name'], $target_path)) { 
    echo "The file ". basename($_FILES['myfile']['name']) . " has been uploaded"; 
} else { 
    echo "There was an error uploading the file, please try again!"; 
} 
+0

Nie mogę powiedzieć, gdzie etykieta tekstu jest ustawiany tutaj, powiedzmy, tekst to "cześć". Gdzie powinien iść ten kod? – sebi

+0

Zaktualizowałem moją odpowiedź .. – akk

+0

OK, dzięki, btw nie użyłem żadnej nazwy zasobu ani znaków, ponieważ nie jestem pewien, co robi. – sebi

4

Wystarczy dodać następującą metodę Na przycisku na kliknięcie:

-(void) uploadImage 
{ 
    prodNam = txtProdName.text; 
    UIImage * img = [UIImage imageNamed:@"SRT2.jpg"]; 
    NSData *imageData = UIImageJPEGRepresentation(img,0.2);  //change Image to NSData 

    if (imageData != nil) 

    { 
     NSString * filenames = [NSString stringWithFormat:@"TextLabel"]; 
     NSLog(@"%@", filenames); 

     NSString *urlString = @"http://dev9.edisbest.com/upload_image.php"; 

     NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ; 
     [request setURL:[NSURL URLWithString:urlString]]; 
     [request setHTTPMethod:@"POST"]; 

     NSString *boundary = @"---------------------------14737809831466499882746641449"; 
     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:NSUTF8StringEncoding]]; 
     [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"filenames\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
     [body appendData:[filenames dataUsingEncoding:NSUTF8StringEncoding]]; 
     [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
     [body appendData:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\"TestEdreamzIpad.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
     [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
     [body appendData:[NSData dataWithData:imageData]]; 
     [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
     [request setHTTPBody:body]; 
     NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 
     NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; 
     NSLog(@"Response : %@",returnString); 

     if([returnString isEqualToString:@"Success ! The file has been uploaded"]) 
     { 
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Success" message:@"Image Saved Successfully" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil]; 

      [alert show]; 
     } 
    NSLog(@"Finish"); 
    } 
} 

Można umieść dowolny obraz w miejscu "SRT2.jpg" w lokalnej hierarchii xcode.

Wszystkie najlepsze ...

+1

Hej, czy możesz też przesłać swój kod PHP? Wypróbowałem Twój kod, ale zawsze było napisane "Wystąpił błąd podczas przesyłania pliku, spróbuj ponownie!" i żadne dane nie pojawiają się na moim serwerze ... –

+2

@ LaurenzGlück: Kod Dla: upload_image.php "iphonetest" to katalog serwera umieszczony w katalogu głównym. Użyj i odpowiedz, czy jest to dla ciebie warte? – Pushkraj

+0

@ LaurenzGlück: Pushkraj

1

Moje serwisy internetowe to ten i działa dobrze!

2

Po prostu użytkownik SVHttp Request do przesyłania plików na serwer.

wyślij imagefile i innych pasz w parametrze:

NSMutableDictionary *data = [[NSMutableDictionary alloc] init]; 
    [data setObject:imageData forKey:@"image"]; 
    [data setObject:@"saveimage" forKey:@"action"]; 
    [data setObject:userId forKey:@"userid"]; 
    [data setObject:@"png" forKey:@"type"]; 

I uczynić Zapytanie:

SVHTTPClient *request = [SVHTTPClient sharedClient]; 

    [request setBasicAuthWithUsername:nil password:nil]; 
    [request setSendParametersAsJSON:NO]; 



    [request POST:[NSString stringWithFormat:@"http://bylyngo.com/getapi/?action=saveimage&userid=%@&type=png",userId] 
     parameters:data 
     progress:^(float progress) { 

      NSLog(@"Uploading (%.0f%%)", progress*100); 

     }]; 

Dostaniesz nawet postępy.

:)

Powiązane problemy