iPhone应用程序向Web服务器发送图片 实例操作

移动开发 iOS
本文介绍的是iPhone应用程序向Web服务器发送图片 实例操作,主要是以代码实现的内容,先来看内容。

iPhone应用程序向Web服务器发送图片 实例操作是本文要介绍的内容,本文主要是以代码实现,来看具体代码。

  1. //把图片转换为NSData    
  2.     UIImage *image = [UIImage imageNamed:@"vim_go.png"];        
  3.     NSData *imageData = UIImagePNGRepresentation(image);    
  4.     // post url    
  5.     NSString *urlString = @"http://10.28.4.162/test-upload.php";    
  6.         
  7.     // setting up the request object now    
  8.     NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];    
  9.     [request setURL:[NSURL URLWithString:urlString]];    
  10.     [request setHTTPMethod:@"POST"];    
  11.     //    
  12.     NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];    
  13.     NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];    
  14.     [request addValue:contentType forHTTPHeaderField: @"Content-Type"];    
  15.     //    
  16.     NSMutableData *body = [NSMutableData data];    
  17.     [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];        
  18.     [body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"vim_go.png\"\r\n"] 
  19. dataUsingEncoding:NSUTF8StringEncoding]];    
  20.     [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];    
  21.     [body appendData:[NSData dataWithData:imageData]];    
  22.     [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];    
  23.     [request setHTTPBody:body];    
  24.     NSLog(@"%@",body);    
  25.     NSLog(@"%@",request);    
  26. NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];    
  27. NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];     
  28.  result_btn.text = returnString;    
  29.  
  30. /Applications/XAMPPdocs    
  31.     
  32. imac:htdocs aitracy$ cat test-upload.php     
  33.     
  34. <?php    
  35. $uploaddir = './upload/';    
  36. echo "recive a image";    
  37. $file = basename($_FILES['userfile']['name']);    
  38. $uploadfile = $uploaddir . $file;    
  39.     
  40. if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {    
  41.     echo "/uploads/{$file}";    
  42. }    
  43. ?>    

小结:iPhone应用程序向Web服务器发送图片 实例操作的内容介绍完了,希望本文对你有所帮助!

责任编辑:zhaolei 来源: 互联网
相关推荐

2017-11-10 08:58:49

Web服务器应用程序

2011-07-22 18:44:45

iPhone HTTPS 服务器

2011-07-26 11:13:15

iPhone PXL

2011-07-19 10:42:41

iPhone 应用程序 模型

2011-07-19 11:12:07

iPhone 控制器

2011-07-19 10:56:15

iPhone 控制器 视图

2011-07-26 13:23:14

iPhone 图片 相册

2019-05-14 09:39:07

Web服务器Web容器应用程序服务器

2011-07-06 16:55:56

iPhone php Push

2009-06-25 17:08:14

2010-04-16 13:32:28

Win2008 R2

2018-08-23 09:16:22

2011-07-21 10:47:37

iPhone Cocoa 委托

2011-09-06 10:58:10

服务器应用程序虚拟化

2011-07-27 14:37:33

iPhone Push Notif 服务器

2018-04-23 14:05:06

LinuxUbuntu 17.1AWFFull

2009-06-01 11:37:46

EquinoxOSGi服务器

2018-03-12 09:13:12

应用程序服务器部署

2009-11-18 09:48:38

Windows PHP

2011-11-16 11:30:08

虚拟化虚拟服务器虚拟服务器备份
点赞
收藏

51CTO技术栈公众号