iOS上传文件参数解析(HTTP header Content-Disposition)

AFNetworking中提供的图片上传方法:

    [_sessionManager POST:uploadUrl parameters:dict constructingBodyWithBlock:^(id  _Nonnull formData) {
        if (imageData) {
            [formData appendPartWithFileData:imageData name:<#name#> fileName:<#fileName#> mimeType:@"jpg"];
        }
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        suc(task,responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        fail(task,error);
    }];

可以看到里边有几个字段:

/**
 Appends the HTTP header `Content-Disposition: file; filename=#{filename}; name=#{name}"` and `Content-Type: #{mimeType}`, followed by the encoded file data and the multipart form boundary.

 @param data The data to be encoded and appended to the form data.
 @param name The name to be associated with the specified data. This parameter must not be `nil`.
 @param fileName The filename to be associated with the specified data. This parameter must not be `nil`.
 @param mimeType The MIME type of the specified data. (For example, the MIME type for a JPEG image is image/jpeg.) For a list of valid MIME types, see http://www.iana.org/assignments/media-types/. This parameter must not be `nil`.
 */
- (void)appendPartWithFileData:(NSData *)data
                          name:(NSString *)name
                      fileName:(NSString *)fileName
                      mimeType:(NSString *)mimeType
{
    NSParameterAssert(name);
    NSParameterAssert(fileName);
    NSParameterAssert(mimeType);

    NSMutableDictionary *mutableHeaders = [NSMutableDictionary dictionary];
    [mutableHeaders setValue:[NSString stringWithFormat:@"form-data; name=\"%@\"; filename=\"%@\"", name, fileName] forKey:@"Content-Disposition"];
    [mutableHeaders setValue:mimeType forKey:@"Content-Type"];

    [self appendPartWithHeaders:mutableHeaders body:data];
}

而 Appends the HTTP header Content-Disposition: file; filename=#{filename}; name=#{name}" and Content-Type: #{mimeType}, followed by the encoded file data and the multipart form boundary.又是什么呢?


可以看出来:

name:就是前后端约定好的描述图片的字段,(比如传入avatar,那就是代表该图片是用户头像,后台在取的时候,也需要根据这个name来取出)

fileName: 上传的文件名称

https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Content-Disposition
https://tools.ietf.org/html/rfc6266

你可能感兴趣的:(iOS上传文件参数解析(HTTP header Content-Disposition))