Ios 使用AFN网络从文档目录路径上载视频/照片
我想从我的文档目录路径上传照片/视频,而无需使用AFNetworking将其与其他参数一起转换为NSData,如果可能的话,可以通过建议我以外的任何其他方式提供任何建议。Nikunj可能是这样,这将帮助您:) NSDictionary*参数=@{ 你的参数Ios 使用AFN网络从文档目录路径上载视频/照片,ios,iphone,afnetworking,Ios,Iphone,Afnetworking,我想从我的文档目录路径上传照片/视频,而无需使用AFNetworking将其与其他参数一起转换为NSData,如果可能的话,可以通过建议我以外的任何其他方式提供任何建议。Nikunj可能是这样,这将帮助您:) NSDictionary*参数=@{ 你的参数 }; //===========================================AFNETWORKING HEADER AFHTTPClient *httpClient =
};
//===========================================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];
}
};
//========================================================================================AFN网络头
AFHTTPClient*httpClient=[[AFHTTPClient alloc]initWithBaseURL:url];
httpClient.parameterEncoding=AFFormURLParameterEncoding;
[httpClient registerHTTPOperationClass:[AFJSONRequestOperation类]];
[httpClient setDefaultHeader:@“接受”值:@“应用程序/json”];
//====================================================简单请求
NSMutableURLRequest*request=[httpClient multipartFormRequestWithMethod:@“POST”路径:kAddMarketProduct参数:params constructingBodyWithBlock:^(id formData)
{
对于(int i=0;iNikunj,这可能会对您有所帮助:)
NSDictionary*参数=@{
你的参数
};
//===========================================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];
}
};
//========================================================================================AFN网络头
AFHTTPClient*httpClient=[[AFHTTPClient alloc]initWithBaseURL:url];
httpClient.parameterEncoding=AFFormURLParameterEncoding;
[httpClient registerHTTPOperationClass:[AFJSONRequestOperation类]];
[httpClient setDefaultHeader:@“接受”值:@“应用程序/json”];
//====================================================简单请求
NSMutableURLRequest*request=[httpClient multipartFormRequestWithMethod:@“POST”路径:kAddMarketProduct参数:params constructingBodyWithBlock:^(id formData)
{
对于(int i=0;您是否使用多部分formdata上载?是的,请检查我的代码,[formdata appendPartWithFileURL:[NSURL URLWithString:videoPath]名称:@“video”文件名:@“video.MOV”mimeType:@“video/quicktime”错误:nil];,我正试图从我的文档目录上传视频,但不幸的是没有上传视频。还有其他建议吗?如果我使用多部分formdata,它工作正常,但当我尝试使用appendPartWithFileURL上传文件时,它不工作,建议?也许,它对你的情况有帮助?@CongTran:-回答得好,非常感谢。superb,正在工作。是否使用多部分formdata上载?是的,请检查我的代码,[formdata appendPartWithFileURL:[NSURL URLWithString:videoPath]名称:@“video”文件名:@“video.MOV”mimeType:@“video/quicktime”错误:无];,我正试图从我的文档目录上传视频,但不幸的是没有上传视频。还有其他建议吗?如果我使用多部分formdata,它工作正常,但当我尝试使用appendPartWithFileURL上传文件时,它不工作,建议?也许,它对你的情况有帮助?@CongTran:-回答得好,非常感谢。sup雇员再培训局,它的工作。谢谢你的建议,但我得到了解决方案。谢谢你的建议,但我得到了解决方案。