I do this with a perl script on the back end. My experience was that its was more cajoling the perl script into working that the PHP side, but that may be because I'm not much of a perl wizard (maybe only level 8 or so :-). I found it was important to declare a filename if you're transferring data like this.

Here is my code:

NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL:aURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30.0];

NSString *boundary = @"----Boundary+AaB03x";

[postRequest setHTTPMethod:@"POST"];
[postRequest addValue:[aURL] absoluteString] forHTTPHeaderField:@"Referer"]; [postRequest setValue:[NSString stringWithFormat:@"multipart/form- data; boundary=%@", boundary, nil] forHTTPHeaderField:@"Content-Type"];

NSEnumerator *dictionaryEnumerator = [postDictionary keyEnumerator];
NSString *currentKey = nil;

NSMutableData *multipartData = [NSMutableData data];

while( (currentKey = [dictionaryEnumerator nextObject]) ) {
[multipartData appendData:[[NSString stringWithFormat:@"[EMAIL PROTECTED] \n", boundary] dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:NO]];
        
        id currentValue = [postDictionary objectForKey:currentKey];
        
if( [currentValue isKindOfClass:[NSData class]] && [NSImageRep imageRepClassForData:currentValue] != nil ) { [multipartData appendData:[[NSString stringWithFormat:@"Content- Disposition: form-data; name=\"[EMAIL PROTECTED]"; filename=\"image.jp2\"\r\n", currentKey] dataUsingEncoding:NSASCIIStringEncoding]];
                
[multipartData appendData:[@"Content-Type: image/jp2\r\n" dataUsingEncoding:NSASCIIStringEncoding]]; [multipartData appendData:[@"Content-Transfer-Encoding: binary\r\n\r \n" dataUsingEncoding:NSASCIIStringEncoding]];
                
                [multipartData appendData:currentValue];
        }               
}

[multipartData appendData:[[NSString stringWithFormat:@"[EMAIL PROTECTED] \n", boundary] dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:NO]];

[postRequest setHTTPBody:multipartData];

Hope this helps,

->Ben

--
Ben Lachman
Acacia Tree Software

http://acaciatreesoftware.com

[EMAIL PROTECTED]
740.590.0009



On Mar 13, 2008, at 4:50 PM, Tom Harrington wrote:

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=\"[EMAIL PROTECTED]@_jpg\"", phone, dateString];
        
        NSMutableData *postBody = [NSMutableData data];
        // Add ----boundary
        [postBody appendData:[[NSString stringWithFormat:@"[EMAIL PROTECTED]", 
boundary]
dataUsingEncoding:NSUTF8StringEncoding]];
        // Add form-data with filename
        [postBody appendData:[[NSString
stringWithFormat:@"Content-Disposition: [EMAIL PROTECTED]", 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:@"[EMAIL PROTECTED]",
boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        
        [req setHTTPBody:postBody];

        NSHTTPURLResponse *res = nil;
        NSError *err = nil;
        
        NSData *data = [NSURLConnection sendSynchronousRequest:req
                                                                                 
returningResponse:&res
                                                                                    
                     error:&err];


--
Tom Harrington
[EMAIL PROTECTED]
AIM: atomicbird1
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/blachman%40mac.com

This email sent to [EMAIL PROTECTED]

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to