【问题标题】:Upload Video/Photo from document directory path using AFNetworking使用 AFNetworking 从文档目录路径上传视频/照片
【发布时间】:2015-11-25 08:36:01
【问题描述】:

我想从我的文档目录路径上传照片/视频,而不是使用 AFNetworking 转换为 NSData 以及其他参数,如果可能的话,可以通过任何其他方式而不是建议我。

【问题讨论】:

  • 你使用multipart-formdata上传吗?
  • 是的,检查我的代码,[formData appendPartWithFileURL:[NSURL URLWithString:videoPath] name:@"video" fileName:@"video.MOV" mimeType:@"video/quicktime" error:nil] ;,我正在尝试从我的文档目录上传视频,但不幸的是没有上传视频。还有其他建议吗?
  • 如果我使用 multipart-formdata 可以正常工作,但是当我尝试使用 appendPartWithFileURL 上传文件时,建议?
  • 也许,它可以帮助你的情况? stackoverflow.com/questions/22180367/…
  • @CongTran :- 很好的答案,非常感谢。太棒了,它的工作原理。

标签: ios iphone afnetworking


【解决方案1】:

Nikunj 也许这会对你有所帮助:)

NSDictionary *params =@{ 你的参数

                        };

//===========================================AFNETWORKING HEADER

AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
httpClient.parameterEncoding = AFFormURLParameterEncoding;
[httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]];
[httpClient setDefaultHeader:@"Accept" value:@"application/json"];

//===============================SIMPLE REQUEST

NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:kAddMarketProduct parameters:params constructingBodyWithBlock:^(id <AFMultipartFormData>formData)
                                {
                                 for (int i = 0; i<[arry_MarketImageArry count]; i++)
                                    {
                                        NSTimeInterval timeInterval = [NSDate timeIntervalSinceReferenceDate];

                                        if ([[[arry_MarketImageArry objectAtIndex:i]valueForKey:@"type"] isEqualToString:@"0"])
                                        {

                                            [formData appendPartWithFileData: [[arry_MarketImageArry objectAtIndex:i]valueForKey:@"data"] name:[NSString stringWithFormat:@"image%d",i+1] fileName:[NSString stringWithFormat:@"%lf-image%d.png",timeInterval,i] mimeType:@"image/jpeg"];


                                        }
                                        else if ([[[arry_MarketImageArry objectAtIndex:i]valueForKey:@"type"] isEqualToString:@"1"])
                                        {

                                            NSData *data_Video=[[NSMutableData alloc]initWithContentsOfURL:[[arry_MarketImageArry objectAtIndex:i]valueForKey:@"data"]];

                                            [formData appendPartWithFileData:data_Video  name:[NSString stringWithFormat:@"image%d",i+1] fileName:[NSString stringWithFormat:@"%lf-video%d.mp4",timeInterval,i] mimeType:@"video/mp4"];


                                            [formData appendPartWithFileData: [[arry_MarketImageArry objectAtIndex:i]valueForKey:@"thumb"] name:[NSString stringWithFormat:@"videoimage%d",i+1] fileName:[NSString stringWithFormat:@"%lf-thumb%d.png",timeInterval,i] mimeType:@"image/jpeg"];


                                        }


                                    }

                                }];



//====================================================RESPONSE


AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {

}];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSError *error = nil;
    NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:&error];

    [delegate.activityIndicator stopAnimating];
    [self ResponseAddMarketProduct:JSON];
}

【讨论】:

  • 感谢您的建议,但我得到了解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-28
  • 1970-01-01
  • 2014-03-19
  • 1970-01-01
  • 1970-01-01
  • 2016-09-30
相关资源
最近更新 更多