2009-05-31 4 views
0

현재 HTTP POST를 통해 서버에 이미지를 업로드하고 있습니다. 아래 코드를 사용하면 모든 것이 잘 작동합니다.Imageshack에 대한 HTTP POST

NSString *UDID = md5([UIDevice currentDevice].uniqueIdentifier); 
NSString *filename = [NSString stringWithFormat:@"%@-%@", UDID, [NSDate date]]; 
NSString *urlString = @"http://taptation.com/stationary_data/index.php"; 
request= [[[NSMutableURLRequest alloc] init] autorelease]; 
[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 *postbody = [NSMutableData data]; 
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@.jpg\"\r\n", filename] dataUsingEncoding:NSUTF8StringEncoding]]; 
[postbody appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
[postbody appendData:[NSData dataWithData:imageData]]; 
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[request setHTTPBody:postbody]; 

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 
returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; 
NSLog(returnString); 

그러나 Image Shacks XML API를 사용하도록 변환하려고하면 아무 것도 반환하지 않습니다. ImageShack의 지시 사항은 다음과 같습니다.

POST를 통해 다음 변수를 imageshack으로 보냅니다. us /index.php

fileupload; (이미지) xml = "예"; (XML 반환 지정) 쿠키; (등록 코드, 선택 사항)

내가 여기에서 어디로 가야하는지 아는 사람이 있습니까?

답변

2

ASIHTTPRequest을 사용하는 것이 좋습니다. 번거롭지 않게 양식 데이터 포스트 본문을 작성하고 큰 이미지를 업로드 할 때 메모리를 절약 할 수 있도록 디스크에서 요청 본문을 스트리밍 할 수 있습니다.

빠른 google은 this으로, /index.php가 아닌 /upload_api.php에 게시해야합니다.

ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithUrl:[NSURL URLWithString:@"http://www.imageshack.us/upload_api.php"]] autorelease]; 
[request setFile:@"/path/to/file" forKey:@"fileupload"]; 
[request setPostValue:@"yes" forKey:@"xml"]; 
[request setPostValue:@"blahblah" forKey:@"cookie"]; 
//It looks as though you probably need these too 
[request setPostValue:@"[email protected]" forKey:@"email"]; 
[request setPostValue:@"blah" forKey:@"key"]; 
[request start]; 
if ([request error]) { 
    NSLog(@"%@",[request error]); 
} else { 
    NSLog([request responseString]); // The xml that got sent back 
} 

경고 : 검증되지 않은이 같은

뭔가 아마 좋은 시작이 될 것입니다!

내가 동기 요청을 사용했기 때문에 비동기 요청 (ASIHTTPRequest가있는 대기열)을 사용해야합니다.

2

잠시만 기다렸지 만 ASIHTTPREQUEST를 사용해야합니다!

- (void)uploadToImageShack { 
NSAutoreleasePool *paul = [[NSAutoreleasePool alloc] init]; 
UIImage *tempImage = self.selectedimage; 
ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.imageshack.us/upload_api.php"]] autorelease]; 
NSData *imageData = UIImagePNGRepresentation(tempImage); 
[request setFile:imageData withFileName:@"image" andContentType:@"image/png" forKey:@"fileupload"]; 
[request setPostValue:@"yes" forKey:@"xml"]; 
[request setPostValue:@"3ZQ7C09K708fce677d9cadee04811cfcbdf63361" forKey:@"key"]; 
[request setUseCookiePersistence:NO]; 
[request startSynchronous]; 
NSError *error = [request error]; 
if (error) { 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil]; 
    [alert show]; 
    [alert release]; 
} 
else if (!error) { 
    if ([request responseString]) { 
     parser = [[NSXMLParser alloc] initWithData:[[NSData alloc] initWithData:[request responseData]]]; 
     [parser setDelegate:self]; 
     [parser parse]; 
    } 
} 
[paul drain]; 
}