Ios uploads files through post
Because iOS cannot upload images through html forms, to upload images, you must implement http requests instead of using post in html forms in other languages.
The format of the http post request for uploading images is as follows:
Java code
- Content-type: multipart/form-data, boundary = AaB03x
-
- -- AaB03x
- Content-disposition: form-data; name = "field1"
-
- Hello Boris!
- -- AaB03x
- Content-disposition: form-data; name = "pic"; filename = "boris.png"
- Content-Type: image/png
-
- ... Contents of boris.png...
- -- AaB03x --
The first line specifies the encoding method of the http post request as multipart/form-data (this is required for uploading files ).
Boundary = AaB03x indicates that AaB03x is the demarcation line. For example, "AaB03x" means a dividing line.
Content-disposition: form-data; name = "field1"
Hello Boris!
This statement indicates the name of a field in the request, such as field1 and the value of the field, such as Hello Boris!
This is similar
Empty lines in the middle are required.
Different fields are separated by a line. The line must be a single line, for example, AaB03x --
The next line of the demarcation line, which is the next field
Content-disposition: form-data; name = "pic"; filename = "boris.png"
Content-Type: image/png
... Contents of boris.png...
-- AaB03x --
The variable pic is declared here, that is, the file to be uploaded. You must specify file name: filename = "boris.png"
In addition, you must specify the file format: Content-Type: image/png in the next line.
... Contents of boris.png... examples of binary content, such as <000000000000000d 49484452 000000b4 000000b4 08020000 00b2af91 65000020 00494441 0000dd79b724 00008c9c8733 55ddb1d5 6a0db000006218401 ......
At the end of an http post request, a line is required, but both the front and back are --: -- AaB03x --
The preceding formats are http specifications. Each blank line and space are required.
The following is the iOS implementation code:
#define HTTP_CONTENT_BOUNDARY @"WANPUSH"-(BOOL)httpPutData:(NSString*)strUrl FilePath:(NSString*)filePath DataType:(NSString*)dataType { strUrl = [strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSURL* url = [NSURL URLWithString:strUrl]; NSData* data = [NSData dataWithContentsOfFile:filePath]; NSString* fileName = [filePath lastPathComponent]; NSString* strBodyBegin = [NSString stringWithFormat:@"--%@\nContent-Disposition: form-data; name=\"%@\"; filename=\"%@\"\nContent-Type: %@\n\n", HTTP_CONTENT_BOUNDARY, @"file", fileName, dataType]; NSString* strBodyEnd = [NSString stringWithFormat:@"\n--%@--",HTTP_CONTENT_BOUNDARY]; NSMutableData *httpBody = [NSMutableData data]; [httpBody appendData:[strBodyBegin dataUsingEncoding:NSUTF8StringEncoding]]; [httpBody appendData:data]; [httpBody appendData:[strBodyEnd dataUsingEncoding:NSUTF8StringEncoding]]; NSMutableURLRequest* httpPutRequest = [[NSMutableURLRequest alloc] init]; [httpPutRequest setURL:url]; [httpPutRequest setHTTPMethod:@"POST"]; [httpPutRequest setTimeoutInterval: 60000]; [httpPutRequest setValue:[NSString stringWithFormat:@"%@", @(httpBody.length)] forHTTPHeaderField:@"Content-Length"]; [httpPutRequest setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@",HTTP_CONTENT_BOUNDARY] forHTTPHeaderField:@"Content-Type"]; httpPutRequest.HTTPBody = httpBody; NSHTTPURLResponse* httpResponse = nil; NSError *error = [[NSError alloc] init]; NSData *responseData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&httpResponse error:&error]; if (httpResponse == nil) { NSLog(@"url: %@\nerror_code: %@", strUrl, error); return NO; } if (httpResponse.statusCode != 200) { NSLog(@"url: %@\nHTTP response: %ld", strUrl, (long)httpResponse.statusCode); return NO; } return YES;}
Background php code: