多部分数据 - 图像上传

时间:2016-11-01 18:43:16

标签: objective-c image image-uploading

我想将图像和字典卸载到服务器,我已经做了一些事情,图像不上传字典是workinfine,这是我的代码,

NSData *jsonData = [NSJSONSerialization dataWithJSONObject: dict
                                                   options:0
                                                     error:nil];
NSString *jsonString =[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSDictionary *headers = @{ @"content-type": @"multipart/form-data; boundary=---011000010111000001101001",

                           @"cache-control": @"no-cache",
                           @"postman-token": @"f93b7bb9-040f-fa7c-8a65-5a4f8e057cfe" };
NSArray *parameters = @[ @{ @"name": @"userDetails", @"value":jsonString  }
                         ,@{ @"name": @"userImage", @"fileName":@{@"0":@{}}}
                         ];
NSString *boundary = @"---011000010111000001101001";

NSError *error;
NSMutableString *body = [NSMutableString string];
for (NSDictionary *param in parameters) {
    [body appendFormat:@"--%@\r\n", boundary];
    if (param[@"fileName"]) {
        [body appendFormat:@"Content-Disposition:form-data; name=\"%@\"; fileName=\"%@\"\r\n", param[@"name"], param[@"fileName"]];
        [body appendFormat:@"Content-Type: %@\r\n\r\n", param[@"contentType"]];
        [body appendFormat:@"%@", [NSString stringWithContentsOfFile:param[@"fileName"] encoding:NSUTF8StringEncoding error:&error]];
   if (error) {
            NSLog(@"%@", error);
        }
    } else {
        [body appendFormat:@"Content-Disposition:form-data; name=\"%@\"\r\n\r\n", param[@"name"]];
        [body appendFormat:@"%@", param[@"value"]];
    }
}
[body appendFormat:@"\r\n--%@--\r\n", boundary];
NSData *postData = [body dataUsingEncoding:NSUTF8StringEncoding];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"---.php"]
                                                       cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                   timeoutInterval:10.0];
[request setHTTPMethod:@"POST"];
[request setAllHTTPHeaderFields:headers];
[request setHTTPBody:postData];

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
        completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                                if (error) {
                                                    NSLog(@"%@", error);
                                                } else {
                                                    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
                                                    NSLog(@"%@", httpResponse);

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

                                                    NSLog(@"%@",stringFromData);}}];
[dataTask resume];}

我从服务器获取的msg成功,上传成功,但图片没有上传,我已经在服务器上签了,请帮我上传图片

0 个答案:

没有答案