IOS development and ios development tutorial

Source: Internet
Author: User

IOS development and ios development tutorial
File Upload steps

1. Set request headers
* Purpose: to tell the server that the content in the Request body is not a common parameter, but contains a file parameter.

[request setValue:@"multipart/form-data; boundary=maljob" forHTTPHeaderField:@"Content-Type"];

2. Set the Request body
* Function: stores parameters (File parameters and non-file parameters)
1> non-file Parameters

[body appendData:MalJobEncode(@"--maljob\r\n")];[body appendData:MalJobEncode(@"Content-Disposition: form-data; name=\"username\"\r\n")];
[Body appendData: MalJobEncode (@ "\ r \ n")]; [body appendData: MalJobEncode (@ "Zhang San")]; [body appendData: malJobEncode (@ "\ r \ n")];

2> file Parameters

[body appendData:MalJobEncode(@"--heima\r\n")];[body appendData:MalJobEncode(@"Content-Disposition: form-data; name=\"file\"; filename=\"test123.png\"\r\n")];[body appendData:MalJobEncode(@"Content-Type: image/png\r\n")];
[body appendData:MalJobEncode(@"\r\n")];[body appendData:imageData];[body appendData:MalJobEncode(@"\r\n")];

3> end mark: Mark of parameter end

[body appendData:MalJobEncode(@"--maljob--\r\n")];
File MIMEType

1. Baidu search

2. Search for an xml file under the server

apache-tomcat-6.0.41\conf\web.xml

3. Get it through Reponse when loading the file

-(NSString *) MIMEType :( NSURL *) url {// 1. create a request NSURLRequest * request = [NSURLRequest requestWithURL: url]; // 2. send a request (response) NSURLResponse * response = nil; [NSURLConnection sendSynchronousRequest: request returningResponse: & response error: nil]; // 3. obtain MIMEType return response. MIMEType ;}
File Upload and encapsulation tools
@ Interface UploadTool: NSObject/*** obtain the MIMEType of the file based on the file path ** @ param url file path ** @ return file MIMEType */-(NSString *) MIMEType :( NSURL *) url; /*** upload a file based on the file name, MIMEType, binary file, and other parameters ** @ param filename file name * @ param mimeType MIMEType * @ param fileData binary file * @ param params non-File other detailed parameters */-(void) upload :( NSString *) filename mimeType :( NSString *) mimeType fileData :( NSData *) fileData params :( NSDictionary *) params; @ end
# Define MJFileBoundary @ "MalJob" # define MJNewLine @ "\ r \ n" # define MJEncode (str) [str dataUsingEncoding: NSUTF8StringEncoding] # import "UploadTool. h "@ implementation UploadTool-(NSString *) MIMEType :( NSURL *) url {// 1. create a request NSURLRequest * request = [NSURLRequest requestWithURL: url]; // 2. send a request (response) NSURLResponse * response = nil; [NSURLConnection sendSynchronousRequest: request returningResponse: & response error: nil]; // 3. obtain MIMEType return response. MIMEType;}-(void) upload :( NSString *) filename mimeType :( NSString *) mimeType fileData :( NSData *) fileData params :( NSDictionary *) params {// 1. request Path NSURL * url = [NSURL URLWithString: @ "http: // 218.83.161.124: 8080/job/upload"]; // 2. create a POST request NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL: url]; request. HTTPMethod = @ "POST"; // 3. set the Request body NSMutableData * body = [NSMutableData data]; // 3. 1. file parameter [body appendData: MJEncode (@ "--")]; [body appendData: MJEncode (MJFileBoundary)]; [body appendData: MJEncode (MJNewLine)]; NSString * disposition = [NSString stringWithFormat: @ "Content-Disposition: form-data; name = \" file \ "; filename = \" % @ \ "", filename]; [body appendData: MJEncode (disposition)]; [body appendData: MJEncode (MJNewLine)]; NSString * type = [NSString stringWithFormat: @ "Content-Type: % @", mimeType]; [body appendData: MJEncode (type)]; [body appendData: MJEncode (MJNewLine)]; [body appendData: MJEncode (MJNewLine)]; [body appendData: fileData]; [body appendData: MJEncode (MJNewLine)]; // 3. 2. non-file parameter [params enumerateKeysAndObjectsUsingBlock: ^ (id key, id obj, BOOL * stop) {[body appendData: MJEncode (@ "--")]; [body appendData: MJEncode (MJFileBoundary)]; [body appendData: MJEncode (MJNewLine)]; NSString * disposition = [NSString stringWithFormat: @ "Content-Disposition: form-data; name = \ "% @ \" ", key]; [body appendData: MJEncode (disposition)]; [body appendData: MJEncode (MJNewLine)]; [body appendData: MJEncode (MJNewLine)]; [body appendData: MJEncode ([obj description])]; [body appendData: MJEncode (MJNewLine)] ;}]; // 3. 3. end tag [body appendData: MJEncode (@ "--")]; [body appendData: MJEncode (MJFileBoundary)]; [body appendData: MJEncode (@ "--")]; [body appendData: MJEncode (MJNewLine)]; request. HTTPBody = body; // 4. set the Request Header (telling the server that the file data is sent to you this time, and telling the server that a file upload request is sent now) NSString * contentType = [NSString stringWithFormat: @ "multipart/form-data; boundary = % @", MJFileBoundary]; [request setValue: contentType forHTTPHeaderField: @ "Content-Type"]; // 5. send request [NSURLConnection failed: request queue: [inclumainqueue] completionHandler: ^ (NSURLResponse * response, NSData * data, NSError * connectionError) {NSDictionary * dict = [NSJSONSerialization failed: data options: NSJSONReadingMutableLeaves error: nil]; MalJobLog (@ "% @", dict) ;}] ;}- (void) upload {// other non-file detailed parameters NSDictionary * params = @{@"":@"",@"":@"",}; NSURL * url = [[NSBundle mainBundle] URLForResource: @ "filename" withExtension: @ "txt"]; NSData * data = [NSData dataWithContentsOfURL: url]; NSString * MIMEType = [self MIMEType: url]; [self upload: @ "filename.txt" mimeType: MIMEType fileData: data params: params];}

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.