iPhone應用程序向Web服務器發(fā)送圖片 實例操作
作者:佚名 
  本文介紹的是iPhone應用程序向Web服務器發(fā)送圖片 實例操作,主要是以代碼實現的內容,先來看內容。
 iPhone應用程序向Web服務器發(fā)送圖片 實例操作是本文要介紹的內容,本文主要是以代碼實現,來看具體代碼。
- //把圖片轉換為NSData
 - UIImage *image = [UIImage imageNamed:@"vim_go.png"];
 - NSData *imageData = UIImagePNGRepresentation(image);
 - // post url
 - NSString *urlString = @"http://10.28.4.162/test-upload.php";
 - // setting up the request object now
 - 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:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"vim_go.png\"\r\n"]
 - 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];
 - NSLog(@"%@",body);
 - NSLog(@"%@",request);
 - NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
 - NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
 - result_btn.text = returnString;
 - /Applications/XAMPPdocs
 - imac:htdocs aitracy$ cat test-upload.php
 - <?php
 - $uploaddir = './upload/';
 - echo "recive a image";
 - $file = basename($_FILES['userfile']['name']);
 - $uploadfile = $uploaddir . $file;
 - if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
 - echo "/uploads/{$file}";
 - }
 - ?>
 
小結:iPhone應用程序向Web服務器發(fā)送圖片 實例操作的內容介紹完了,希望本文對你有所幫助!
責任編輯:zhaolei 
                    來源:
                    互聯(lián)網
 














 
 
 


 
 
 
 