2013-11-03 63 views
0

我使用DropBox的核心API的OAuth 2.0認證,我試圖上傳的UIImage通過它的核心API圖片上傳到DropBox的服務器核心API送禮者錯誤的iOS

https://api-content.dropbox.com/1/files//

https://www.dropbox.com/developers/core/docs發現( /文件(POST))

我的代碼是

-(void)uploadData:(NSData *)imageAsData and:(NSString *)fileName{ 

NSString *urlString=[NSString stringWithFormat:@"https://api-content.dropbox.com/1/files/dropbox/Apps?access_token=%@",dropBoxToken]; 


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", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

[body appendData:[[NSString stringWithFormat:@"Content-Disposition: attachment; name=\"attachement\"; filename=\"%@\";",fileName] dataUsingEncoding:NSUTF8StringEncoding]]; 


[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[NSData dataWithData:imageAsData]]; 
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithFormat:@"--%@--\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(@"returnString=%@",returnString); 

其中imageAsData的圖像和文件名的NSData的是「abc.jp G」。 returnString的

輸出爲{「錯誤」:「文件的參數值‘無’是無效的」}

我不知道爲什麼會出現這個問題,請幫助我。

回答

1

請爲您節省很多麻煩,並使用更簡單的files_put端點。 (有它的文檔中推薦的一個原因。)你的代碼的

粗糙編輯(未經測試):

-(void)uploadData:(NSData *)imageAsData and:(NSString *)filePath { 

    NSString *urlString=[NSString stringWithFormat: 
     @"https://api-content.dropbox.com/1/files_put/dropbox/%@?access_token=%@", 
     filePath, dropBoxToken]; 

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

    [request setHTTPBody:imageAsData]; 

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

    NSLog(@"returnString=%@",returnString); 
} 
+0

按預期工作。非常感謝您節省了我的時間。 –