FROM : Tom Harrington
DATE : Thu Mar 13 21:50:40 2008
I'm trying to upload a file with the NSURL* API, but on the server end
the PHP code is unable to decode the $_FILES array. The PHP code
works fine when posted from an HTML form (and from clients on other
platforms).
Traffic sniffed on the wire looks good as far as I can tell, so what
would be the problem? The code in question follows.
NSString *urlString = @"http://server:8080/php/post.php";
NSURL *url = [NSURL URLWithString:urlString];
NSString *requestMethod = @"POST";
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
[req setHTTPMethod:requestMethod];
NSString *headerFieldName;
NSString *headerFieldValue;
NSString *boundary = @"-----------------------------1234567890";
headerFieldName = @"Content-Type";
headerFieldValue = [NSString stringWithFormat:@"multipart/form-data;
boundary=%@", boundary];
[req addValue:headerFieldValue forHTTPHeaderField:headerFieldName];
NSString *phone = @"7195554321";
NSDate *currentDate = [NSDate date];
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"yyyyMMddHHmmss"];
NSString *dateString = [dateFormatter stringFromDate:currentDate];
// Will look something like this: form-data; name="file";
filename="7195554321_1_20080311132327_jpg"
headerFieldValue = [NSString stringWithFormat:@"form-data;
name=\"file\"; filename=\"%@_1_%@_jpg\"", phone, dateString];
NSMutableData *postBody = [NSMutableData data];
// Add ----boundary
[postBody appendData:[[NSString stringWithFormat:@"%@\r\n", boundary]
dataUsingEncoding:NSUTF8StringEncoding]];
// Add form-data with filename
[postBody appendData:[[NSString
stringWithFormat:@"Content-Disposition: %@\r\n", headerFieldValue]
dataUsingEncoding:NSUTF8StringEncoding]];
// Add content-type
[postBody appendData:[[NSString stringWithString:@"Content-Type:
null\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
// Add image data
[postBody appendData:imageData];
// Add ----boundary--
[postBody appendData:[[NSString stringWithFormat:@"\r\n%@--\r\n",
boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[req setHTTPBody:postBody];
NSHTTPURLResponse *res = nil;
NSError *err = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:req
returningResponse:&res
error:&err];
--
Tom Harrington
<email_removed>
AIM: atomicbird1
DATE : Thu Mar 13 21:50:40 2008
I'm trying to upload a file with the NSURL* API, but on the server end
the PHP code is unable to decode the $_FILES array. The PHP code
works fine when posted from an HTML form (and from clients on other
platforms).
Traffic sniffed on the wire looks good as far as I can tell, so what
would be the problem? The code in question follows.
NSString *urlString = @"http://server:8080/php/post.php";
NSURL *url = [NSURL URLWithString:urlString];
NSString *requestMethod = @"POST";
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
[req setHTTPMethod:requestMethod];
NSString *headerFieldName;
NSString *headerFieldValue;
NSString *boundary = @"-----------------------------1234567890";
headerFieldName = @"Content-Type";
headerFieldValue = [NSString stringWithFormat:@"multipart/form-data;
boundary=%@", boundary];
[req addValue:headerFieldValue forHTTPHeaderField:headerFieldName];
NSString *phone = @"7195554321";
NSDate *currentDate = [NSDate date];
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"yyyyMMddHHmmss"];
NSString *dateString = [dateFormatter stringFromDate:currentDate];
// Will look something like this: form-data; name="file";
filename="7195554321_1_20080311132327_jpg"
headerFieldValue = [NSString stringWithFormat:@"form-data;
name=\"file\"; filename=\"%@_1_%@_jpg\"", phone, dateString];
NSMutableData *postBody = [NSMutableData data];
// Add ----boundary
[postBody appendData:[[NSString stringWithFormat:@"%@\r\n", boundary]
dataUsingEncoding:NSUTF8StringEncoding]];
// Add form-data with filename
[postBody appendData:[[NSString
stringWithFormat:@"Content-Disposition: %@\r\n", headerFieldValue]
dataUsingEncoding:NSUTF8StringEncoding]];
// Add content-type
[postBody appendData:[[NSString stringWithString:@"Content-Type:
null\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
// Add image data
[postBody appendData:imageData];
// Add ----boundary--
[postBody appendData:[[NSString stringWithFormat:@"\r\n%@--\r\n",
boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[req setHTTPBody:postBody];
NSHTTPURLResponse *res = nil;
NSError *err = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:req
returningResponse:&res
error:&err];
--
Tom Harrington
<email_removed>
AIM: atomicbird1
| Related mails | Author | Date |
|---|---|---|
| Tom Harrington | Mar 13, 21:50 | |
| Tom Harrington | Mar 13, 22:02 | |
| Tom Harrington | Mar 13, 22:41 | |
| Ben Lachman | Mar 14, 07:43 | |
| Tom Harrington | Mar 14, 16:49 |






Cocoa mail archive

