我从盒子开发者网站得到以下信息。
curl https://upload.box.com/api/2.0/files/content \
-H "Authorization: Bearer ACCESS_TOKEN" \
-F filename=@FILE_NAME \
-F parent_id=PARENT_FOLDER_ID
- F 表示什么?如何制作请求网址?
我从盒子开发者网站得到以下信息。
curl https://upload.box.com/api/2.0/files/content \
-H "Authorization: Bearer ACCESS_TOKEN" \
-F filename=@FILE_NAME \
-F parent_id=PARENT_FOLDER_ID
在这里我得到了解决方案。
NSString * accessToken = [[arrUseraccounts objectAtIndex:[DropboxDownloadFileViewControlller getSharedInstance].index] objectForKey:@"acces_token"];
NSString * filename = [NSString stringWithFormat:@"@%@",[[filePathsArray objectAtIndex:k]objectForKey:@"PdfName"]];
NSString * boxParentId = [DetailViewController getSharedInstance].folderPath;
NSString *str = [NSString stringWithFormat:@"https://upload.box.com/api/2.0/files/content?access_token=%@&filename=%@&parent_id=%@",accessToken,filename,boxParentId];
ASIFormDataRequest *postParams = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:str]];
postParams.delegate = self ;
postParams.userInfo = [NSDictionary dictionaryWithObject:@"upload" forKey:@"id"];
// set the path of the file where it is saved (local Path)
[postParams setFile:[[filePathsArray objectAtIndex:k]objectForKey:@"PdfPath"] forKey:@"filename"];
[postParams startAsynchronous];
将文件上传到盒子非常容易。请在下面找到代码。
- (void)performSampleUpload:(id)sender
{
BoxFileBlock fileBlock = ^(BoxFile *file)
{
[self fetchFolderItemsWithFolderID:self.folderID name:self.navigationController.title];
dispatch_sync(dispatch_get_main_queue(), ^{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"File Upload Successful" message:[NSString stringWithFormat:@"File has id: %@", file.modelID] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
});
};
BoxAPIJSONFailureBlock failureBlock = ^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, NSDictionary *JSONDictionary)
{
BOXLog(@"status code: %i", response.statusCode);
BOXLog(@"upload response JSON: %@", JSONDictionary);
};
BoxFilesRequestBuilder *builder = [[BoxFilesRequestBuilder alloc] init];
builder.name = @"Logo_Box_Blue_Whitebg_480x480.jpg";
builder.parentID = self.folderID;
NSString *path = [[NSBundle mainBundle] pathForResource:@"Logo_Box_Blue_Whitebg_480x480.jpg" ofType:nil];
NSInputStream *inputStream = [NSInputStream inputStreamWithFileAtPath:path];
NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil];
long long contentLength = [[fileAttributes objectForKey:NSFileSize] longLongValue];
[[BoxSDK sharedSDK].filesManager uploadFileWithInputStream:inputStream contentLength:contentLength MIMEType:nil requestBuilder:builder success:fileBlock failure:failureBlock progress:nil];
}
享受编码......干杯。
-F:模拟用户按下提交按钮的填写表单。这会导致 curl 根据 RFC 2388 标准使用 Content-Type multipart/form-data 发布数据。-F 接受像 -F "name=contents" 这样的参数来从文件中读取内容,使用 <@filename> 作为内容。指定文件时,您还可以通过在文件名后面附加 ';type=' 来指定文件内容类型。
使用 -F 模拟填写表单。假设您在表单中填写了三个字段。一个字段是要发布的文件名,一个字段是您的姓名,一个字段是文件描述。我们要发布我们编写的名为“cooltext.txt”的文件。
CUrl 示例(发送几个字段和文件):
curl -F "file=@cooltext.txt" -F "yourname=Daniel" \
-F "filedescription=Cool text file with cool text inside" \
http://www.post.com/postit.cgi
Objective-c 示例(发送一个表单值):
/*
* Create Mutable URL request and get method to Post with content type as a form
*/
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.exmaple.com/post"]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
/*
* build http body
*/
NSData *data = [@"firstname" dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:data];
[request setValue:[NSString stringWithFormat:@"%u", data.length] forHTTPHeaderField:@"Content-Length"];
/*
* Make request and return result
*/
NSError *error;
NSHTTPURLResponse *response;
NSData *result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (error) {
NSLog(@"request error: %@", error);
return;
}
NSString *formattedResult = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];
NSLog(@"result: %@ http headers: %@", formattedResult, response.allHeaderFields);
参考:
CUrl 手册页
RFC 2388 标准