将图像上传到服务器ios [英] Upload image to server ios

查看:125
本文介绍了将图像上传到服务器ios的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的是用post方法将图像和文本发送到我的数据库

What i trying to do is send image and text to my database in post method

- (void)uploadpic
{
    NSData *imageData = UIImageJPEGRepresentation(self.imageview.image, 90);
    //   NSString *action=@"tempinsert";

    NSString *s=@"54.316130";
    NSString *s2=@"9.950930";
    NSString *facebook=@"12345";
    NSString *cate=@"1";

    NSString *action=@"insert";

    NSString *note=@"Iiiiiii";


    NSString *str=[NSString stringWithFormat:@"my url.php"];
    NSString *urlString = [NSString stringWithFormat:@"%@",str];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];
    NSMutableData *body = [NSMutableData data];
    NSString *boundary = @"---------------------------14737809831466499882746641449";
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];

    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Disposition: form-data; name=\"uploaded_file\"; filename=\"popup1.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

    [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:imageData]];
    [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

    //  parameter username

    //    "lat=%@&long=%@&sdkuserid=%@&cat=%@&action=%@"]; My parameters


    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"sdkuserid\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

    [body appendData:[facebook dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];


    //  parameter token
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"insert\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

    [body appendData:[action dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];


    // parameter method
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"cat\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

    [body appendData:[cate dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];


    //parameter method

    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"long\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

    [body appendData:[s2 dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

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


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


    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    // NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
    NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:returnData options:NSJSONReadingMutableLeaves error:nil];
    //
    NSLog(@" -------   >%@",returnData);
    NSLog(@" -------   >%@",dict);
}

输出

  take photo[1295:31484]  -------   ><0d0a0d0a 0d0a0d0a>
  take photo[1295:31484]  -------   >(null)

我的输出总是空的。我试图将图像和少量文本发送到服务器。虽然我没有任何这样的验证,但是我没有尝试使用更少的数据。但我每次都得到null。

My out is always null.I ma trying to send image and few text to server .Although i donnot have any such validation that firld cannot be empty so i have tried with less data.But I get null everytime.

推荐答案

//Make sure you are sending a valid Base64 image data.    
NSData *data = UIImageJPEGRepresentation(imageToSend,1);
    NSString *imageData = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];

//Make a request
dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSError *error;
            NSData *imageJsonData = [NSJSONSerialization dataWithJSONObject:dictionaryWithImageDataStringAndSomeText
                                                                    options:NSJSONWritingPrettyPrinted
                                                                      error:&error];
            NSString *urlString = yourImageUploadUrlToSomeServer;
            NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
            [request setURL:[NSURL URLWithString:urlString]];
            [request setHTTPMethod:@"POST"];
            [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
            [request setValue:[NSString stringWithFormat:@"%lu",(unsigned long)imageJsonData.length] forHTTPHeaderField:@"Content-Length"];
            [request setHTTPBody:imageJsonData];

            NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
            NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

            NSLog(@"Image upload response: %@",returnString);
        });

//At your server decode back your image data, if you're using java do something like this:
byte[] data = Base64.decode(imageData);
InputStream in = new ByteArrayInputStream(data);
BufferedImage bi = ImageIO.read(in);
ImageIO.write(bi, "jpg", new File(to/some/path))

这篇关于将图像上传到服务器ios的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆