【问题标题】:How can I cleanly POST a file to an API endpoint without an external library?如何在没有外部库的情况下干净地将文件发布到 API 端点?
【发布时间】:2013-10-29 15:27:42
【问题描述】:

我正在尝试在我的应用程序后台仅使用本机代码将文件发布到 API 端点。我可以使用以下代码,但这对我来说似乎非常笨拙。

有没有一种更简洁的方法可以通过本机 Objective-C 代码实现这一目标?

这是我尝试过的:

- (void)sendMyImage:(NSData *)image
{

    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:true];

    NSString *previewApiUrl = @"URL_OF_MY_ENDPOINT"]
    NSMutableURLRequest *request= [[NSMutableURLRequest alloc] init];

    [request setURL:[NSURL URLWithString:previewApiUrl]];
    [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", @"MYFILENAME"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postbody appendData:[[NSString stringWithFormat:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postbody appendData:image];
    [postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    // Include the auth headers
    [request setValue:@"MYVALUE1" forHTTPHeaderField:@"CUSTOM_AUTH_HEADER_1"];
    [request setValue:@"MYVALUE2" forHTTPHeaderField:@"CUSTOM_AUTH_HEADER_2"];

    [request setHTTPBody:postbody];

    NSOperationQueue *mainQueue = [[NSOperationQueue alloc] init];

    [NSURLConnection sendAsynchronousRequest:request queue:mainQueue completionHandler:^(NSURLResponse *response, NSData *responseData, NSError *error) {
        NSHTTPURLResponse *urlResponse = (NSHTTPURLResponse *)response;
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:false];
        if (!error) {

            NSString *jsonString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; //parse my JSON response
        }
        else {
                // Handle error condition
        }
    }];
}

【问题讨论】:

  • 我不这么认为,你需要设置所有这些参数,这就是世界使用第三方API的原因:)
  • 只是好奇:您反对使用 3rd 方库的原因是什么?
  • @AaronBrager 我正在构建一个 .framework 文件/捆绑包,它将为合作应用程序发布并希望消除依赖关系
  • 参考这篇文章上传图片或任何其他文件here for objective c - 祝你好运
  • @Nick 您应该能够完全在您的实现中使用像 AFNetworking 这样的框架,并将其排除在您的界面之外。这将为您带来大量积极的第 3 方支持的好处,如果您想停止使用它,您只需修改您的实现,其他任何人都不需要更改他们的代码。

标签: ios objective-c rest


【解决方案1】:

有没有更简洁的方法来实现这一点,只使用本机 Objective-C 代码?

不,这就是你的做法。您可以通过将部分逻辑封装到单独的方法中来使其看起来更清晰。您还有一些可以删除的冗长内容。 (为什么使用[NSData dataWithData:image]而不是image?为什么只添加一个操作时设置maxConcurrentOperationCount?)

【讨论】:

  • 感谢您的反馈。
猜你喜欢
  • 2011-05-15
  • 1970-01-01
  • 2017-11-12
  • 2013-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多