使用PHP在ios上通过JSON在webserver上上传图像 [英] Upload image on webserver through JSON in ios using PHP

查看:137
本文介绍了使用PHP在ios上通过JSON在webserver上上传图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用我在网络服务器上传用户数据的应用。

I am having an app in which I am uploading user's data on webserver.

我使用以下代码将数据传递给网络服务器,代码如下。

I am using the below code to pass the data to webserver with the code below.

-(IBAction)btnRegister:(id)sender
{
    jsonSP = [SBJSON new];
    jsonSP.humanReadable = YES;

     NSString *service = @"/register.php";
    NSData *imageData = UIImagePNGRepresentation([UIImage imageNamed:@"register_btn.png"]);
    [Base64 initialize];
    NSString *imageString = [Base64 encode:imageData];
    NSLog(@"%@",imageString);


    NSString *requestString = [NSString stringWithFormat:@"{\"?username\"=\"%@\"&\"user_password\"=\"%@\"&\"first_name\"=\"%@\"&\"email\"=\"%@\"&\"profile_photo_link\"=\"%@\"\"}",@"Test",@"Test",@"Test",@"Test@mmm.com",imageString];
    NSData *requestData = [NSData dataWithBytes: [requestString UTF8String] length: [requestString length]];

    NSString *fileLoc = [[NSBundle mainBundle] pathForResource:@"URLName" ofType:@"plist"];
    NSDictionary *fileContents = [[NSDictionary alloc] initWithContentsOfFile:fileLoc];
    NSString *urlLoc = [fileContents objectForKey:@"URL"];
     urlLoc = [urlLoc stringByAppendingString:service];
    NSLog(@"URL : %@",urlLoc);
   // urlLoc = [urlLoc stringByAppendingString:@"?username=abcd&password=abcd"];
    urlLoc = [urlLoc stringByAppendingString:requestString];
    urlLoc = [urlLoc stringByReplacingOccurrencesOfString:@"{" withString:@""];
    urlLoc = [urlLoc stringByReplacingOccurrencesOfString:@"}" withString:@""];
    urlLoc = [urlLoc stringByReplacingOccurrencesOfString:@"\"" withString:@""];
    NSLog(@"URL : %@",urlLoc);
    // [fileContents release]; //o_r
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: urlLoc]];
    NSString *postLength = [NSString stringWithFormat:@"%d", [requestData length]];
    [request setHTTPMethod: @"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody: requestData];

    //    self.connection = [NSURLConnection connectionWithRequest:request delegate:self];

    NSError *respError = nil;
    NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: &respError ];
    NSLog(@"response is %@",returnData);


    // [request release];
    if (respError)
    {
        //[customSpinner hide:YES];

        UIAlertView *alt=[[UIAlertView alloc]initWithTitle:@"Internet connection is not Available!" message:@"Check your network connectivity" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [alt show];
        //[alt release];


    }
    else
    {
        NSString *responseString = [[NSString alloc] initWithData:returnData encoding: NSUTF8StringEncoding];
        NSLog(@"response string %@",responseString);
    }
}

我成功地将数据发送到网络服务器而无需图像。

I am successfully able to send the data to webserver without the image.

但如果通过编码Base64上传带有上述代码的图像,则数据不会上传到网络服务器上。 它在NSLog中给我一个错误,说请求查询字符串太长。

But if upload the image with the code shown above by encoding Base64 then the data is not uploaded on webserver. It gives me an error in NSLog saying request query string too long.

我已经导入了Base64.h和.m文件。

I have already imported Base64.h and .m files.

我也想用图像传递数据。

I want to pass the data with image also.

我该怎么做?

我知道有很多链接,但我想以这种方式通过,所以请告诉我我在这里做错了什么?

I know there are lots of links but I want to pass it this way so please tell me what is wrong I am doing here?

请帮助我。

提前致谢。

推荐答案

我使用以下代码处理多部分表单数据。

I did it working with the following code with multipart form data.

responseData = [NSMutableData data];
    NSString *service = @"http:///registerUser.php";

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:service]];
    [request setHTTPMethod:@"POST"];

//创建边界...

// Create a boundry...

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

//创建帖子的正文

NSMutableData *body = [NSMutableData data];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Disposition: form-data; name=\"profile_photo_link\"; filename=\"ipodfile.png\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

 UIImageView *imgvUserImage = [[UIImageView alloc]init];
    imgvUserImage.image= [UIImage imageNamed:@"register_btn.png"];

    NSData * imageData=[NSData dataWithData:UIImagePNGRepresentation(imgvUserImage.image)];

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


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

    [body appendData:[[NSString stringWithFormat:@"%@",txtUserName.text] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

//将帖子正文设置为reqeust

// setting the body of the post to the reqeust

 [request setHTTPBody:body];
    postconnection=[[NSURLConnection alloc] initWithRequest:request delegate:self];

**对于回复

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    [responseData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [responseData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    [self removeProgressIndicator];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    if (connection == postconnection)
    {
        NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
        NSLog(@"%@",responseString);
        //   NSDictionary *deserializedData = [responseString objectFromJSONString];
        //  NSString *string = [deserializedData objectForKey:@"Status"];


    }
}

这篇关于使用PHP在ios上通过JSON在webserver上上传图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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