douxing1353 2016-03-18 04:32
浏览 70
已采纳

无法在IOS上的服务器上上传图像

I am not able to upload images on php server from my IOS app.

Here is my code..

NSString *urlString = @"My URl";

 NSURL* requestURL = [NSURL URLWithString:urlString];

// setting up the request object now

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;

 [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];


    // NSString *boundary = @"---------------------------147378098314876653456641449";
    NSString *BoundaryConstant = @"----------V2ymHFg03ehbqgZCaKO6jy";

    NSString* FileParamConstant = @"profileimage";


    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",BoundaryConstant];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];

        ///---

    // post body
    NSMutableData *body = [NSMutableData data];

    // add params (all params are strings)
    for (NSString *param in dicSubmit)
    {
        [body appendData:[[NSString stringWithFormat:@"--%@
", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"

", param] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"%@
", [dicSubmit objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];
    }


    NSData *imageData = UIImageJPEGRepresentation(self.imgprofile.image, 0.0f);

    if (imageData)
    {
        [body appendData:[[NSString stringWithFormat:@"--%@
", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"editprofileimage1.png\"
", FileParamConstant] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[@"Content-Type: image/jpeg

" dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:imageData];
        [body appendData:[[NSString stringWithFormat:@"
"] dataUsingEncoding:NSUTF8StringEncoding]];
    }



    [body appendData:[[NSString stringWithFormat:@"--%@--
", BoundaryConstant] 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"];

    // set URL
    // [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeBlack];
    [request setURL:requestURL];
    [[NSURLConnection alloc] initWithRequest:request delegate:self];
  • 写回答

1条回答 默认 最新

  • doudongdang4483 2016-03-18 05:56
    关注

    Try to using AFNetworking

    AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@"your url"]];
    
            NSData *imageData = UIImagePNGRepresentation(self.m_clientPhoto.image);
    
            NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
                [dict setValue:self.nameTextField.text forKey:@"name"];
                [dict setValue:self.countryCode1 forKey:@"countrycode"];
                [dict setValue:self.phone1 forKey:@"phone"];
    
            manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
    
            AFHTTPRequestOperation *op = [manager POST:@"rest.of.url" parameters:dict constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    
    
                [formData appendPartWithFileData:imageData name:@"img" fileName:@"photo.jpg" mimeType:@"image/jpeg"];
    
            } success:^(AFHTTPRequestOperation *operation, id responseObject) {
    
                NSLog(@"Success: %@ ***** %@", operation.responseString, responseObject);
    
    
            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    
                    NSLog(@"Error: %@ ***** %@", operation.responseString, error);
    
                }];
    
        [op start];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码