上传图像到网络服务器从ios到PHP
我有一个应用程序,我在上传一些数据到Web服务器。上传图像到网络服务器从ios到PHP
这是我从谷歌的链接中获得的代码。
UIImage * img = [UIImage imageNamed:@"register_btn.png"];
NSData *imageData = UIImageJPEGRepresentation(img, 90);
NSString *urlString = @"http://tes.in/testservices/User.php";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"------------------------1473780983146499882746641449";
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 stringWithFormat:@"Content-Disposition: form-data;filename=\a.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Type: application/json\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];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString =[[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
//profile_photo_link
NSLog(@"%@",returnString);
但我没有得到什么,我需要在代码中传递。
我的照片上传参数是“photo_link”。我已经给出了PHP web服务的链接。
我知道这里有很多问题,但我不知道在哪里传递我的参数以及如何检查请求字符串。
如果可能的话,请任何人都可以给我PHP的代码来解码图像。
请帮帮我。
在此先感谢。
试试这个代码:
-(void)uploadImageOnServer:(NSData *)imageData forId:(NSString *)imageId
{
NSURL *strURL = @"http://webserver.com/....";//give your URL here
// setting up the request object now
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
[request setURL:strURL];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------147378098314876653456641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
/*
now lets create the body of the post
*/
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
NSString *stringData = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"Documents\"; filename=\%@.jpg\r\n",imageId];
[body appendData: [stringData dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"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]];
// setting the body of the post to the request
[request setHTTPBody:body];
// now lets make the connection to the web
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"%@",returnString);
}
“文档”是您要的图像保存到后端服务器的文件夹名称。
祝你好运!
感谢您的回复。什么是util和方法?我是否需要导入任何框架? – 2014-10-08 07:46:12
@PatelManthansorry bro,就像发布API的操作一样。请忽略此并检查我的更新答案。 – 2014-10-08 07:48:49
@PatelManthan你试过我的答案,它对你有用吗? – 2014-10-08 09:18:07
你有没有想过使用AFNETWorking?它大大简化了这样的请求 – Smiless 2014-10-08 07:39:13
如果你可以给我这个代码,那会很好。我已经给出了我的参数和web服务的链接。 – 2014-10-08 07:40:34
为什么downvote?这是一个法律问题,具有所有的局限性和关于程序设计的问题。那么为什么人们会倒下。只是不明白。 – 2014-10-09 04:18:19