本文介绍了Objective-C Box 2.0文件上传-问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去几天来,我一直在尝试使用Box上传文件。我知道我做错了,但是看不到它是什么。

I've been trying to get file upload working with Box for the past few days. I know I'm doing something wrong, but just can't see what it is.

我已尽我最大的努力减少了代码,

I've reduced my code down as much as I possibly can, to just this:

// Configure the request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"https://api.box.com/2.0/files/data"]];
[request setHTTPMethod:@"POST"];

[request setValue:boxAuthString forHTTPHeaderField:@"Authorization"];

// Setu up the request
NSString *boundary = [NSString stringWithString:@"--PLEASEHELPMEGETTHISWORKING"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@\r\n",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];

NSMutableData *body = [NSMutableData data];

// Add the info and data for the file.
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition:form-data;name=\"filename\";filename=\"testfile.txt\"\r\n"]
                      dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type:text/plain\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Hello"] dataUsingEncoding:NSUTF8StringEncoding]];

// Add the info and data for the target folder.
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition:form-data;name=\"folder_id\"\r\n\r\n"]
                  dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"0"] dataUsingEncoding:NSASCIIStringEncoding]];

// Close the body and set to the request
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding: NSUTF8StringEncoding]];
[request setHTTPBody:body];

// now lets make the connection to the web
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

NSLog(@"%@", returnString);

但是,我仍然返回:
{ type: error ,状态:404,代码:未找到,帮助网址: http://developers.box.com/docs/#errors,消息:未找到, request_id: 10130600215xxxxxxxxxxx}

But, I still get a return of:{"type":"error","status":404,"code":"not_found","help_url":"http://developers.box.com/docs/#errors","message":"Not Found","request_id":"10130600215xxxxxxxxxxx"}

请有人帮我指出正确的方向,因为当我使用POSTMAN或cURL时,我可以使它正常工作-显然这是一个问题与我的代码。我怀疑这与'folder_id'有关,但似乎无法诊断出实际原因。

Please can someone help point me in the right direction, as when I use POSTMAN or cURL, I can get it to work - so it is obviously an issue with my code. I suspect it is something to do with the 'folder_id', but can't seem to diagnose the actual cause.

推荐答案

已修复它。问题出在我的边界构造代码中。我会得到:

Fixed it. The problem was in my 'boundary' construction code. I'd got:

NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@\r\n",boundary];


而且应该有:

NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];


找出错误的终止符 \r\n。那好吧。仅仅浪费了大约4个小时。 :-)

Spot the incorrect termination '\r\n'. Oh well. Only wasted about 4 hours on this. :-)

这篇关于Objective-C Box 2.0文件上传-问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 01:16