将图像上传到服务器时出错

时间:2017-10-09 11:58:36

标签: ios objective-c

我想将图片上传到服务器。上传到服务器时出现502错误。以下是我得到的错误

 { status code: 502, headers {
    Connection = "keep-alive";
    "Content-Length" = 181;
    "Content-Type" = "text/html";
    Date = "Mon, 09 Oct 2017 10:47:49 GMT";
    Server = "nginx/1.4.6 (Ubuntu)";
} }

错误内容类型显示 text / html ,但我将其设置为multipart / form-data。

如果我删除Content-Type中的边界我的服务器抛出105错误,后端开发人员说105意味着内容类型不匹配。

下面是我的代码:

   -(void)uploadImage:(UIImage *)image withBlock:(void (^)(NSError *error))block {
    self.apiPath = postProfilePath;
    NSString *requestUrl = [NSString stringWithFormat:@"%@%@", self.baseUrl, self.apiPath];
    requestUrl = [requestUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSMutableURLRequest *request = [self getRequestFor:requestUrl withImage:image cookies:[[AppManager sharedManager] storedCookies]];

    self.dataTask = [[AdmeNetworkManager sharedNetworkManager] requestWithRequestObject:request completionHandler:^(NSURLResponse *response, id responseData, NSError *error) {

        if(error)   {
             NSLog(@"error while uploading image %@",error);
            if(block) {
                block(error);
            }
        } else {
            NSDictionary *responseDict = (NSDictionary *)[AdmeUtility parseJsonData:responseData];
             NSLog(@"success %@",responseDict);
            if ([AdmeNetworkError isSuccessResponse:responseDict]) {
                if(block) {
                    block(error);
                }
            } else {
                if(block) {
                     NSLog(@"error upload Image %@",error);
                    error = [NSError errorWithDomain:@"adme" code:-3003 userInfo:responseDict];
                    block(error);
                }
            }
        }
    }];
}




 -(NSMutableURLRequest *)getRequestFor:(NSString *)requestUrl withImage:(UIImage *)image cookies:(NSDictionary *)cookies{
    NSError *error = nil;
    //    Request serializer
    NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:requestUrl parameters:nil error:&error];
    if (cookies) {
        NSString *token = [NSString stringWithFormat:@"Bearer %@",[cookies objectForKey:@"token"]];
        NSString *userId = [NSString stringWithFormat:@"%@",[cookies objectForKey:@"id"]];
        [request setValue:token forHTTPHeaderField:@"token"];
        [request setValue:userId forHTTPHeaderField:@"appuserid"];
        NSLog(@"headers for image %@ %@",token, userId);
    }

    NSString *boundary = @"---------------------------14737809831466499882746641449";
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
    [request setValue:contentType forHTTPHeaderField: @"Content-Type"];
    //[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    // post body
    NSMutableData *body = [NSMutableData data];

    NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
   // NSString *fileNameStr = [NSString stringWithFormat:@"%@.jpg", imageName];
    // add params (all params are strings)
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=%@\r\n\r\n", @"imageCaption"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"%@\r\n", @"Some Caption"] dataUsingEncoding:NSUTF8StringEncoding]];

    // add image data
    if (imageData) {

        [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
         [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@.jpg\"\r\n", @"file"] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
        //[body appendData:imageData];
        [body appendData:[NSData dataWithData:imageData]];
        [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    }

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

    // setting the body of the post to the reqeust
    [request setHTTPBody:body];

    // set the content-length
    NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[body length]];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];

    return request;
}

1 个答案:

答案 0 :(得分:0)

502 =错误网关错误。您的nginx.conf使用一个或多个proxy_pass指令与后端通信,并且该后端处于脱机状态或未侦听proxy_pass指令指定的同一地址/端口。