2009-05-31 61 views
0

我目前正在通過HTTP POST將圖像上傳到我的服務器。一切工作正常使用下面的代碼。HTTP POST到Imageshack

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 =「yes」; (指定XML的返回) cookie; (註冊碼,可選)

有誰知道我應該從哪裏出發?

回答

2

您可能會考慮使用ASIHTTPRequest,因爲它將爲您創建一個表單數據帖子主體,並且可以從磁盤傳輸請求主體,因此您可以在上傳大圖時節省內存。

快速谷歌發現this,這似乎建議你應張貼到/upload_api.php而不是/index.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]; 
}