Pripyat
Pripyat

Reputation: 2937

Sending Data - NSMutableURLRequest

I am sending an e-mail via a PHP script. This works fine using NSMutableURLRequest, but I would like to attach an image to the e-mail as well.

Here's the code I'm using:

    NSMutableURLRequest *request = 
    [[NSMutableURLRequest alloc] initWithURL:
    [NSURL URLWithString:@"http://example.com/email.php"]];

    [request setHTTPMethod:@"POST"];

    NSString*subject = [NSString stringWithFormat:@"Images from %@",[[UIDevice currentDevice] name]];   

    NSString*to = [[NSUserDefaults standardUserDefaults] objectForKey:@"PreferredPrinterEPrint"];   

    NSString *postString = [NSString stringWithFormat:@"to=%@&message=%@&subject=%@",to,@"hi",subject];

    [request setValue:[NSString 
                       stringWithFormat:@"%d", [postString length]] 
    forHTTPHeaderField:@"Content-length"];

    [request setHTTPBody:[postString 
                          dataUsingEncoding:NSUTF8StringEncoding]];

    [[NSURLConnection alloc] initWithRequest:request delegate:self];

I've seen an article here which describes sending images, but I don't know how to combine the two. Could someone be so kind an help me out?

Thank you!

Upvotes: 0

Views: 485

Answers (2)

malex
malex

Reputation: 10096

Example of sending JPEG image:

NSMutableURLRequest *request =  [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://example.com/email.php"]];

[request setHTTPMethod:@"POST"];

NSString *boundary = @"Some string as you like";
NSString *value = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request setValue:value forHTTPHeaderField:@"Content-Type"];

NSString *subject = [NSString stringWithFormat:@"Images from %@", [[UIDevice currentDevice] name]];   
NSString *to = [[NSUserDefaults standardUserDefaults] objectForKey:@"PreferredPrinterEPrint"];   

NSData *fileData = [NSData dataWithContentsOfFile:<file path>]

NSMutableString *postDataString = @"".mutableCopy;
[postDataString appendFormat:@"--%@\r\n", boundary];
[postDataString appendFormat:@"Content-Disposition: form-data; name=\"to\"\r\n"];
[postDataString appendFormat:@"\r\n%@\r\n", to];
[postDataString appendFormat:@"--%@\r\n", boundary];
[postDataString appendFormat:@"Content-Disposition: form-data; name=\"message\"\r\n"];
[postDataString appendFormat:@"\r\n%@\r\n", @"hi"];
[postDataString appendFormat:@"--%@\r\n", boundary];
[postDataString appendFormat:@"Content-Disposition: form-data; name=\"subject\"\r\n"];
[postDataString appendFormat:@"\r\n%@\r\n", subject];
[postDataString appendFormat:@"--%@\r\n", boundary];
[postDataString appendFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", <file name>, <file name>];
[postDataString appendFormat:@"Content-Type: image/jpg\r\n"];
[postDataString appendFormat:@"Content-Transfer-Encoding: binary\r\n\r\n"];

NSMutableData *postData = [postDataString dataUsingEncoding:NSUTF8StringEncoding].mutableCopy;

[postData appendData:fileData];

[postData appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

[request setValue:[NSString stringWithFormat:@"%d", postDataString.length]  forHTTPHeaderField:@"Content-length"];
[request setHTTPBody:postData];

[[NSURLConnection alloc] initWithRequest:request delegate:self];

Upvotes: 0

Matthew Frederick
Matthew Frederick

Reputation: 22305

ASIHTTPRequest has a class called ASIFormDataRequest that makes it incredibly easy to post data and includes the ability to attach files with a single line. I highly recommend it.

Upvotes: 1

Related Questions