[IOS multithreading & amp; network, ios Multithreading

Source: Internet
Author: User

[IOS multithreading & Network, ios Multithreading
A. File UploadIdea: to send file data to the server, you must manually set the request header: Content size Content-Length & Content Type Content-Type Request body: the file data file upload format requirements are very strict, files must be loaded to the memory at one time. Therefore, files can only be uploaded to small files.B. Implementation1. set POST request (1) Use POST Request Method (2) Set request header content length, content type, split line (3) Set Request body NSMutableData * body = [NSMutableData data]; split line + line feed content description + line feed content type + line feed file binary data + line feed split line -- multipart/form-data content 1/** get the MIMEType of the local file */2 -( void) getMIMEType {3 // Socket implements resumable upload 4 5 // apache-tomcat-6.0.41/conf/web. mimeType 6 // UIImage * image = [UIImage imageNamed: @ "test"]; 7 // NSData * filedata = UIImagePNGRepresentation (image ); 8 // [self upload: @ "file" filename: @ "test.png" mimeType: @ "image/png" data: filedata parmas: @ {@ "username ": @ "123"}]; 9 10 // send a request to the local file 11 NSURL * fileurl = [[NSBundle mainBundle] URLForResource: @ "itcast.txt" withExtension: nil]; 12 NSURLRequest * request = [NSURLRequest requestWithURL: fileurl]; 13 NSURLResponse * repsonse = nil; 14 NSData * data = [NSURLConnection sendSynchronousRequest: request failed: & repsonse error: nil] 15 16 // obtain the mimeType17 NSLog (@ "% @", repsonse. MIMEType); 18 [self upload: @ "file" filename: @ "itcast.txt" mimeType: repsonse. MIMEType data: data parmas: @ {@ "username": @ "tom", @ "type": @ "xml"}]; 19}Appendix: common file types: MIMEType 1 // 2 // ViewController. m 3 // UploadFileDemo 4 // 5 // Created by hellovoidworld on 15/1/28. 6 // Copyright (c) 2015 hellovoidworld. all rights reserved. 7 // 8 9 # import "ViewController. h "10 11 # define UTF8Encode (str) [str dataUsingEncoding: NSUTF8StringEncoding] 12 13 @ interface ViewController () 14 15-(IBAction) upload; 16 17 @ end 18 19 @ implementation ViewController 20 21 -(Void) viewDidLoad {22 [super viewDidLoad]; 23 // Do any additional setup after loading the view, typically from a nib. 24} 25 26-(void) didReceiveMemoryWarning {27 [super didReceiveMemoryWarning]; 28 // Dispose of any resources that can be recreated. 29} 30 31-(IBAction) upload {32 UIImage * image = [UIImage imageNamed: @ "IMG_0413"]; 33 NSData * imageData = UIImagePNGRepresentation (image); 3 4 [self upload: @ "uploadedFile" filename: @ "IMG_0413.PNG" mimeType: @ "image/png" data: imageData parmas: nil]; 35} 36 37-(void) upload :( NSString *) name filename :( NSString *) filename mimeType :( NSString *) mimeType data :( NSData *) data parmas :( NSDictionary *) params 38 {39 // File upload 40 NSURL * url = [NSURL URLWithString: @ "http: // 192.168.0.21: 8080/MyTestServer/upload"]; 41 NSMutableURLRequest * request = [NSM UtableURLRequest requestWithURL: url]; 42 request. HTTPMethod = @ "POST"; 43 44 // set the Request body 45 NSMutableData * body = [NSMutableData data]; 46. 47. * start flag 49 [body appendData: UTF8Encode (@ "-- HelloVoidWorldBoundary \ r \ n")]; 50 // name: Specifies the parameter name (must be consistent with the server side) 51 // filename: file Name 52 NSString * disposition = [NSString stringWithFormat: @ "Content-Disposition: form-data; nam E = \ "% @ \"; filename = \ "% @ \" \ r \ n ", name, filename]; 53 [body appendData: UTF8Encode (disposition)]; 54 NSString * type = [NSString stringWithFormat: @ "Content-Type: % @ \ r \ n", mimeType]; 55 [body appendData: UTF8Encode (type)]; 56 57 [body appendData: UTF8Encode (@ "\ r \ n")]; 58 [body appendData: data]; 59 [body appendData: UTF8Encode (@ "\ r \ n")]; 60 61/**************** common parameters ***************/62 [params enumerateKeysAn DObjectsUsingBlock: ^ (id key, id obj, BOOL * stop) {63 // mark of the start of the parameter 64 [body appendData: UTF8Encode (@ "-- HelloVoidWorldBoundary \ r \ n")]; 65 NSString * disposition = [NSString stringWithFormat: @ "Content-Disposition: form-data; name = \ "% @ \" \ r \ n ", key]; 66 [body appendData: UTF8Encode (disposition)]; 67 68 [body appendData: UTF8Encode (@ "\ r \ n")]; 69 [body appendData: UTF8Encode (obj)]; 70 [body appendData: UTF8Enco De (@ "\ r \ n")]; 71}]; 72 73/**************************/74 // HelloVoidWorldBoundary -- \ r \ n 75 [body appendData: UTF8Encode (@ "-- HelloVoidWorldBoundary -- \ r \ n")]; 76 request. HTTPBody = body; 77 78 // set the length of the request Header 79 // 80 [request setValue: [NSString stringWithFormat: @ "% zd", body. length] forHTTPHeaderField: @ "Content-Length"]; 81 // declare that this POST request is a file upload 82 [request setValue: @ "multipart/form-data; bo Undary = response "response: @" Content-Type "]; 83 84 // send request 85 [NSURLConnection sendAsynchronousRequest: request queue: [NSOperationQueue mainQueue] completionHandler: ^ (NSURLResponse * response, NSData * data, NSError * connectionError) {86 NSLog (@ "Start upload ~~~ "); 87 if (data) {88 NSDictionary * dict = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableLeaves error: nil]; 89 NSLog (@" % @ ", dict ); 90} else {91 NSLog (@ "Upload Failed"); 92} 93}]; 94} 95 96/** get the MIMEType */97-(void) of the local file) getMIMEType {98 // Socket implements resumable upload 99 100 // apache-tomcat-6.0.41/conf/web. mimeType101 // UIImage * image = [UIImage imageNamed: @ "test"]; 102 // NSData * filedata = UIImagePNGRepresentation (image); 103 // [self upload: @ "file" filename: @ "test.png" mimeType: @ "image/png" data: filedata parmas :@{@ "username": @ "123"}]; 104 105 // send a request to the local file 106 NSURL * fileurl = [[NSBundle mainBundle] URLForResource: @ "itcast.txt" withExtension: nil]; 107 NSURLRequest * request = [NSURLRequest requestWithURL: fileurl]; 108 NSURLResponse * repsonse = nil; 109 NSData * data = [NSURLConnection sendSynchronousRequest: request returningResponse: & repsonse error: nil]; 110 111 // obtain mimeType112 NSLog (@ "% @", repsonse. MIMEType); 113 [self upload: @ "file" filename: @ "itcast.txt" mimeType: repsonse. MIMEType data: data parmas: @ {@ "username": @ "tom", @ "type": @ "xml"}]; 114} 115 116 117 @ end

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.