//获取签名
[[QLMNetworkTools sharedTools] requestWithType:GET andUrlStr:[NSString stringWithFormat:@"%@%@",Test_Net_URL,Voide_getSign_Port] andParams:nil andSuccess:^(id responseObject) {
if ([responseObject[@"status"] integerValue] == 200) {
QNConfiguration *config = [QNConfiguration build:^(QNConfigurationBuilder *builder) {
builder.useHttps = YES;
}];
QNUploadManager *upManager = [[QNUploadManager alloc]initWithConfiguration:config];
//将音乐上传到七牛
[upManager putFile:musicPath key:musicPath token:responseObject[@"data"][@"upToken"] complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
if(info.ok) {
AFHTTPSessionManager * session=[[AFHTTPSessionManager alloc]init]; session.requestSerializer=[AFHTTPRequestSerializer serializer];
session.responseSerializer=[AFHTTPResponseSerializer serializer]; [session POST:[NSString stringWithFormat:@"%@%@",Test_Net_URL,Upload_music_port] parameters:paramters constructingBodyWithBlock:^(id _Nonnull formData) {
NSData *musicData = [NSData dataWithContentsOfFile:musicPath];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyyMMddHHmmss";
NSString *str = [formatter stringFromDate:[NSDate date]];
NSString *fileName = [NSString stringWithFormat:@"%@.mp3", str];
//上传的参数(以文件流的格式)
[formData appendPartWithFileData:musicData
name:@"file"
fileName:fileName
mimeType:@"application/octer-stream"];
} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
id json = [NSJSONSerialization JSONObjectWithData: responseObject options:0 error:nil];
[SVProgressHUD dismiss];
if ([json[@"status"] integerValue] == 200) {
NSLog(@"上传音乐成功 -- %@",json)
}else{
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"%@",error);
[SVProgressHUD dismiss];
}];
}else{
NSLog(@"失败");
//如果失败,这里可以把info信息上报自己的服务器,便于后面分析上传错误原因
}
NSLog(@"info ===== %@", info);
NSLog(@"resp ===== %@", resp);
} option:nil];
}else{
}
} andFailture:^(NSError *error) {
NSLog(@"%@",error);
}];
网友评论