上传文件的时候需要获取文件的MIMEType,可以使用以下接口:
其中path为文件的本地路径
+ (NSString *)fileMIMETypeURLSessionWithPath:(NSString *)path {
NSURL *url = [NSURL fileURLWithPath:path];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
__block NSString *mimeType = nil;
NSURLSessionTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
mimeType = response.MIMEType;
dispatch_semaphore_signal(semaphore);
}];
[task resume];
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
return mimeType;
}
网友评论