美文网首页
获取本地文件MIMEType

获取本地文件MIMEType

作者: Leoeoo | 来源:发表于2020-08-10 18:29 被阅读0次

    上传文件的时候需要获取文件的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;
    }
    

    相关文章

      网友评论

          本文标题:获取本地文件MIMEType

          本文链接:https://www.haomeiwen.com/subject/auojdktx.html