2013-03-27 65 views
2

我試圖將兩個圖像上傳到我的Web服務器。下面有什麼,我現在要做的,上傳一個圖像:將兩個圖像上傳到WebServer

NSData *imageData = UIImagePNGRepresentation(imageToSend); 
// setting up the URL to post to 
NSString *urlString = @"http://www.myweb.com.br/_resources/testeDir.php"; 

// setting up the request object now 
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"]; 

/* 
now lets create the body of the post 
*/ 
NSMutableData *body = [NSMutableData data]; 
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\"ipodfile.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]]; 
// setting the body of the post to the reqeust 
[request setHTTPBody:body]; 

//Create the connection with the string URL and kick it off 
NSURLConnection *urlConnection = [NSURLConnection connectionWithRequest:request delegate:self]; 
[urlConnection start]; 

有了一個形象的作品不錯,但,我怎麼能在同一時間發送兩個圖像?

一種選擇是使用AFNetworking,但我不能讓它的工作,到目前爲止,繼承人是我的嘗試:

AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:urlString]; 
NSMutableURLRequest *requestHTTP = [httpClient multipartFormRequestWithMethod:@"POST" path:@"/" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) { 
    [formData appendPartWithFileData:imageData name:@"userfile" fileName:@"ipodfile.jpg" mimeType:@"image/jpeg"]; 
}]; 

AFJSONRequestOperation *operation = [[AFJSONRequestOperation alloc] initWithRequest:requestHTTP]; 
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) { 

    NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite); 
    //CGFloat progress = ((CGFloat)totalBytesWritten)/totalBytesExpectedToWrite * 100; 


}]; 
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { 
    NSLog(@"Upload succes"); 
} failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
    NSLog(@"Upload failed"); 
}]; 
[operation start]; 

繼承人我的PHP代碼:

<?php 
    $currentDirectory = getcwd(); 
    $uploaddir = $currentDirectory."/4/HD/"; 
    $uploaddir2 = $currentDirectory."/4/thumb/"; 
    $file = basename($_FILES['userfile']['name']); 
    $uploadfile = $uploaddir . $file; 
    $file2 = basename($_FILES['userfile2']['name2']); 
    $uploadfile2 = $uploaddir2 . $file2; 

    if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) { 
     echo $uploaddir."{$file}"; 
     } 

    if (move_uploaded_file($_FILES['userfile2']['tmp_name'], $uploadfile2)) { 
     echo $uploaddir2."{$file2}"; 
     } 
?> 
+1

爲什麼不在相同的請求中添加另一個圖像? – nsgulliver 2013-03-27 13:44:50

回答

1

又一個圖像添加到該請求具有不同的名稱。

NSMutableData *body = [NSMutableData data]; 
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\"ipodfile.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",boundary] dataUsingEncoding:NSUTF8StringEncoding]];//MOD HERE 
    //and add 

    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[@"Content-Disposition: form-data; name=\"userfile2\"; filename=\"ipodfile2.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]]; 

不要關閉身體,直到你加入這兩個文件- % - \ r \ n

[編輯]這是我要做的事:

if([[NSFileManager defaultManager] fileExistsAtPath:path]) 
    { 
     NSData *data = [NSData dataWithContentsOfFile:path]; 
     //NSLog(@"image present:%d",[data length]); 

     [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
     [body appendData:[@"Content-Disposition: attachment; name=\"file1\"; filename=\"image.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
     [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
     //[body appendData:[self encryptThisData:data]]; 
     [body appendData:data]; 
     [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
    } 
    if([[NSFileManager defaultManager] fileExistsAtPath:videoPath]) 
    { 
     NSString *content = [NSString stringWithContentsOfFile:videoPath encoding:NSUTF8StringEncoding error:nil]; 
     NSURL *pathURL = [NSURL URLWithString:content]; 
     ////NSLog(@"cur url is:%@",pathURL); 
     NSData *data = [NSData dataWithContentsOfURL:pathURL]; 
     //NSLog(@"data len is:%d",[data length]); 

     [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
     [body appendData:[@"Content-Disposition: attachment; name=\"file3\"; filename=\"video.mov\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
     [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
     [body appendData:data]; 
     [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
    } 
// close form 
    [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

    // set request body 
    [request setHTTPBody:body]; 
+0

['body appendData:[[NSString stringWithFormat:@「\ r \ n」,boundary] dataUsingEncoding:NSUTF8StringEncoding]]; // MOD HERE'shuold be'[body appendData:[[NSString stringWithFormat:@「\ r \ n - %@「,邊界] dataUsingEncoding:NSUTF8StringEncoding]]; // MOD HERE'does not it? @GabCas – darkman 2013-03-27 14:32:12

+0

驗證我編輯的答案。 – GabCas 2013-03-27 14:35:23