从iOS上传图像到PHP服务器

编程入门 行业动态 更新时间:2024-10-19 10:25:50
本文介绍了从iOS上传图像到PHP服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我知道这个问题也早先被问过,但我的问题有点不同。

I know this question has been asked earlier as well but my issue is a bit different.

我想将图像上传到PHP服务器,我想要从iOS发送更多参数和图像。 我在谷歌上搜索并找到了两个解决方案:

I want to upload an image to the PHP server and i want to send more parameters along with an image from an iOS. I searched on the google and found two solutions:

  • 我们要么发送图片 Base64 JSON 中的编码字符串。推荐链接。

  • Either we will send an image as Base64 encoded string in JSON. Referred link.

    或者我们将使用表单数据将图像上传到服务器。我已经推荐了这个链接。如果有人这样引用我,那么请帮我在这个API中添加更多参数。

    Or we will upload an image to server using form data. I have referred this link. If someone refers me this way, then please help me to add more parameters in this API.

    现在我的问题是,哪一个是将图像上传到服务器的最佳方式,我必须在同一个Web服务调用中发送更多参数(用户名,密码和更多详细信息)。

    Now my question is, which one is the best way to upload an image to the server and i have to send more parameters (username, password and more details) in the same web service call.

    提前致谢。

    推荐答案

    您可以将图像从iOS App上传到 PHP服务器喜欢这两种方式:

    You can upload image from iOS App to PHP server like this two way:

    使用新的AFNetworking :

    Using New AFNetworking :

    #import "AFHTTPRequestOperation.h" #import "AFHTTPRequestOperationManager.h" NSString *stringUrl =@"www.myserverurl/file/uloaddetails.php?" NSString *string =@"myimageurkstrn/img/myimage.png" NSURL *filePath = [NSURL fileURLWithPath:string]; NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:userid,@"id",String_FullName,@"fname",String_Email,@"emailid",String_City,@"city",String_Country,@"country",String_City,@"state",String_TextView,@"bio", nil]; AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; [manager POST:stringUrl parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { [formData appendPartWithFileURL:filePath name:@"userfile" error:nil];//here userfile is a paramiter for your image } success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"%@",[responseObject valueForKey:@"Root"]); Alert_Success_fail = [[UIAlertView alloc] initWithTitle:@"myappname" message:string delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil]; [Alert_Success_fail show]; } failure:^(AFHTTPRequestOperation *operation, NSError *error) { Alert_Success_fail = [[UIAlertView alloc] initWithTitle:@"myappname" message:[error localizedDescription] delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil]; [Alert_Success_fail show]; }];

    第二次使用NSURLConnection:

    -(void)uploadImage { NSData *imageData = UIImagePNGRepresentation(yourImage); NSString *urlString = [ NSString stringWithFormat:@"yourUploadImageURl.php?intid=%@",1]; NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; [request setURL:[NSURL URLWithString:urlString]]; [request setHTTPMethod:@"POST"]; NSString *boundary = [NSString stringWithString:@"---------------------------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:[[NSString stringWithString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@\"\r\n", 1]] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[NSData dataWithData:imageData]]; [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [request setHTTPBody:body]; [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; }

    这种方式适用于将图像从app上传到php服务器希望这会有所帮助对你而言。

    This both way working fine for uploading image from app to php server hope this helps for you.

  • 更多推荐

    从iOS上传图像到PHP服务器

    本文发布于:2023-11-26 13:21:20,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1634010.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:图像   上传   服务器   iOS   PHP

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!