NSURLSession的使用

2019-05-28  本文已影响0人  智狸

1.NSURLSession之上传文件:

- (void)uploadFile {

        NSString *boundary = [self generateBoundaryString];

            // 请求的Url

            NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"server upload file url"]];

            [requestsetHTTPMethod:@"POST"];

            // 设置ContentType

            NSString*contentType = [NSStringstringWithFormat:@"multipart/form-data; boundary=%@", boundary];

            [requestsetValue:contentTypeforHTTPHeaderField:@"Content-Type"];

            NSString*fieldName =@"movie.mp4";

            NSData*httpBody = [selfcreateBodyWithBoundary:boundaryparameters:nilpaths:@[exportSession.outputURL.path]fieldName:fieldName];

           [[[NSURLSession sharedSession] uploadTaskWithRequest:request fromData:httpBody completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

                NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

                NSLog(@"%@\n%@", result,error);

            }]resume];

}

- (NSData*)createBodyWithBoundary:(NSString*)boundary

                        parameters:(NSDictionary*)parameters

                             paths:(NSArray*)paths

                         fieldName:(NSString*)fieldName {

    NSMutableData *httpBody = [NSMutableData data];

    // 文本参数

    [parametersenumerateKeysAndObjectsUsingBlock:^(NSString*parameterKey,NSString*parameterValue,BOOL*stop) {

        [httpBodyappendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

        [httpBodyappendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", parameterKey] dataUsingEncoding:NSUTF8StringEncoding]];

        [httpBodyappendData:[[NSString stringWithFormat:@"%@\r\n", parameterValue] dataUsingEncoding:NSUTF8StringEncoding]];

    }];

    // 本地文件的NSData

    for(NSString*pathinpaths) {

        NSString*filename  = [pathlastPathComponent];

        NSData  *data      = [NSData dataWithContentsOfFile:path];

        NSString*mimetype  = [selfmimeTypeForPath:path];

        [httpBodyappendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

        [httpBodyappendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", fieldName, filename] dataUsingEncoding:NSUTF8StringEncoding]];

        [httpBodyappendData:[[NSString stringWithFormat:@"Content-Type: %@\r\n\r\n", mimetype] dataUsingEncoding:NSUTF8StringEncoding]];

        [httpBodyappendData:data];

        [httpBodyappendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

    }

    [httpBodyappendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    returnhttpBody;

}

- (NSString*)mimeTypeForPath:(NSString*)path {

    CFStringRef extension = (__bridge CFStringRef)[path pathExtension];

    CFStringRef UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, extension, NULL);

    NSString *mimetype = CFBridgingRelease(UTTypeCopyPreferredTagWithClass(UTI, kUTTagClassMIMEType));

    CFRelease(UTI);

    returnmimetype;

}

- (NSString*)generateBoundaryString {

    return [NSString stringWithFormat:@"Boundary-%@", [[NSUUID UUID] UUIDString]];

}

上一篇下一篇

猜你喜欢

热点阅读