代码之家  ›  专栏  ›  技术社区  ›  Kishore Kumar

上传图像时获取可可3840错误?

  •  1
  • Kishore Kumar  · 技术社区  · 9 年前

    这里我试图将图像发送到服务器端,但它给了我错误

        if(picking)
                {
                    NSLog(@"entering in to image side");
                    [body appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
                    [body appendData:[[NSString stringWithFormat:@"content-Disposition:form-data;name=\"pic\"\r\n\r\n filename=imageName.jpg\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
                    [body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
                    [body appendData:[NSData dataWithData:picking]];
                    [body appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
                }
    
                [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
                [request setHTTPBody:body];
                NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[body length]];
                [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    
    [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc]init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
                dispatch_async(dispatch_get_main_queue(),^{
    
                    if([data length]>0 && connectionError==nil)
                    {
                        NSError *error;
    
    
                        NSMutableDictionary *jsonResponse=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
    
                        if(error){
                            [Utitlity alertFunction:@"Warning" message:[error localizedDescription]];
                        }
                        else
                        {
                            [self requestComplete:jsonResponse];
                        }
                    }
    

    这个图像出现在注册屏幕上,如果我没有在注册屏幕中选择图片,意味着客户注册成功。而选择图像则会产生可可3048错误。

    1 回复  |  直到 9 年前
        1
  •  0
  •   Abhinav    9 年前

    问题在您的以下声明中。根据Apple文档 NSJSONReadingMutableContainers 选项应用于数组和字典。请尝试使用 NSJSONReadingAllowFragments .

    NSMutableDictionary *jsonResponse=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
    

    常量NSJSONReadingMutabbleContainers指定数组和 字典被创建为可变对象。

    在iOS 5.0及更高版本中提供。NSJSONReadingMutabbleLeaves指定 JSON对象图中的叶字符串被创建为 NSMutabbleString。

    在iOS 5.0及更高版本中提供。NSJSONReadingAllowFragments指定 解析器应该允许不是 NSArray或NSDictionary的实例。

    在iOS 5.0及更高版本中提供。