2014-02-25 24 views
0

我使用AFNetworking 2.0上傳單個圖像存儲從UIImgePhotoLibrary中選擇的圖像,我把它轉移到NSData我可以看到,當我試圖上傳它的NSData時,NSData保存一些數據返回沒有圖像到達服務器,甚至沒有花時間在一秒鐘內返回錯誤。在過去的幾天裏,我對這個問題非常震驚,我不知道到底在哪裏登錄?NSData有大小,但圖像未達到服務器返回空

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 


    UIImage *image =[info objectForKey:UIImagePickerControllerOriginalImage]; 
    NSData *imageData = UIImageJPEGRepresentation(image, 1.0); 

    AFJSONRequestSerializer *requestSerializer = [AFJSONRequestSerializer serializer]; 
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 


    manager.responseSerializer = [AFHTTPResponseSerializer serializer]; 
    manager.requestSerializer = requestSerializer; 
    [requestSerializer setValue:@"image/jpg" forHTTPHeaderField:@"Content-Type"]; 
    [requestSerializer setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[imageData length]] forHTTPHeaderField:@"CONTENT_LENGTH"]; 

    [manager POST:@"http://xxxxx.com/appphotos/fileupload.php" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { 
     [formData appendPartWithFileData:imageData name:@"image" fileName:@"image/jpeg" mimeType:@"image/jpeg"]; 
    } success:^(AFHTTPRequestOperation *operation, id responseObject) { 
     NSLog(@"Success: %@", operation.responseString); 

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
     NSLog(@"Error: %@", [error debugDescription]); 
     NSLog(@"Error: %@", [error localizedDescription]); 
     NSLog(@"response :%@",operation.responseString); 
    }]; 



    [picker dismissViewControllerAnimated:YES completion:Nil]; 

} 

可以任何一個幫助或請讓我知道,如果我沒有錯誤錯誤的地方。 thx

+0

看看實際發送到網絡分析器如Charles Proxy的服務器響應是什麼。 – zaph

+0

@Zaph我分析過它顯示圖像內容 – Yohan

+0

您已經使用了網絡分析儀,您的回覆沒有說明您分析的方式/內容。因此,服務器不喜歡POST的某些方面,您需要仔細閱讀服務器上傳文檔並檢查發送的內容是否完全符合這些要求。 – zaph

回答

0

我創建了一個應用程序,可以將照片上傳到服務器。一旦我將下面的代碼包含進去,我只能將它運行起來,但這可能會因服務器而異,因此您可能必須執行Zaph建議的操作,並在您的服務器文檔中進行閱讀。這就是說,嘗試一下代碼,也許你會幸運:)

NSMutableString *myString = [[NSMutableString alloc] initWithString:@"http://www.yourdomain.com/yourServerMethodHere/"]; 
    //[myString appendString:@"anyAdditionalURLpartHere"]; 
    NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:myString] 
                   cachePolicy:NSURLRequestUseProtocolCachePolicy 
                  timeoutInterval:60]; 
    [urlRequest setHTTPMethod:@"POST"]; 
    NSString *boundary = @"----WebKitFormBoundaryQkZe6RUJZ2xbzXLB"; 
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; 
    [urlRequest addValue:contentType forHTTPHeaderField: @"Content-Type"]; 
    NSMutableData *body = [NSMutableData data]; 
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[@"Content-Disposition: form-data; name=\"YOURserverVariableStoringTheIncomingData\" filename=\"image001.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[@"Content-Type: image/jpg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[NSData dataWithData:photoData]]; 
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [urlRequest setHTTPBody:body]; 
相關問題