Post sends uiimage data to the server.

Source: Internet
Author: User

 

//This example send email with attachment using server side    //So iPhone will fill form and send proper request to web server using POST method    //For test you can use attached sendEmail.php       -(NSMutableData *)generateDataFromText:(NSString *)dataText fieldName:(NSString *)fieldName{        NSString *post = [NSString stringWithFormat:@"--AaB03x/r/nContent-Disposition: form-data; name=/"%@/"/r/n/r/n", fieldName];        // Get the post header int ASCII format:        NSData *postHeaderData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];              // Generate the mutable data variable:        NSMutableData *postData = [[NSMutableData alloc] initWithLength:[postHeaderData length] ];        [postData setData:postHeaderData];              NSData *uploadData = [dataText dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];              // Add the text:         [postData appendData: uploadData];              // Add the closing boundry:         [postData appendData: [@"/r/n" dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]];              // Return the post data:         return postData;    }       - (NSData *)generatePostDataForData:(NSData *)uploadData fileName:(NSString *)fileName    {        // Generate the post header:               NSString *post = [NSString stringWithFormat:@"--AaB03x/r/nContent-Disposition: form-data; name=/"attachment/"; filename=/"%@/"/r/nContent-Type: video/3gpp/r/n/r/n", fileName];              // Get the post header int ASCII format:        NSData *postHeaderData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];              // Generate the mutable data variable:        NSMutableData *postData = [[NSMutableData alloc] initWithLength:[postHeaderData length] ];        [postData setData:postHeaderData];              // Add the image:         [postData appendData: uploadData];              // Add the closing boundry:        [postData appendData: [@"/r/n--AaB03x--" dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]];              // Return the post data:         return postData;    }       -(void)sendEmailTo:(NSString *)email name:(NSString *)name {                 NSString *emailBody = @"Just some HTML text"              //Fill some text fields         NSMutableData *postData = [self generateDataFromText:email fieldName:@"to_email"];        [postData appendData:[self generateDataFromText:@"10000000" fieldName:@"MAX_FILE_SIZE"]];        [postData appendData:[self generateDataFromText:name fieldName:@"to_name"]];        [postData appendData:[self generateDataFromText:email fieldName:@"to_email"]];        [postData appendData:[self generateDataFromText:[[NSUserDefaults standardUserDefaults] stringForKey:@"name_preference"] fieldName:@"from_name"]];        [postData appendData:[self generateDataFromText:[[NSUserDefaults standardUserDefaults] stringForKey:@"email_preference"] fieldName:@"from_email"]];        [postData appendData:[self generateDataFromText:currentEmail.emailSubject fieldName:@"subject"]];        [postData appendData:[self generateDataFromText:emailBody fieldName:@"body"]];        [postData appendData:[self generateDataFromText:email fieldName:@"to_email"]];           //Prepare data for file         NSData *dataObj = [NSData dataWithContentsOfFile:@"/link/to/file"];        [postData appendData:[self generatePostDataForData:dataObj fileName:@"fileName"]];                    // Setup the request:         NSMutableURLRequest *uploadRequest = [[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:kLinkToPostFile] cachePolicy: NSURLRequestReloadIgnoringLocalCacheData timeoutInterval: 30 ] autorelease];        [uploadRequest setHTTPMethod:@"POST"];        [uploadRequest setValue:[NSString stringWithFormat:@"%d", [postData length]] forHTTPHeaderField:@"Content-Length"];        [uploadRequest setValue:@"multipart/form-data; boundary=AaB03x" forHTTPHeaderField:@"Content-Type"];        [uploadRequest setHTTPBody: postData];              NSData *responseData = [NSURLConnection sendSynchronousRequest:uploadRequest returningResponse:nil error:nil];        NSLog([[NSString alloc] initWithData:responseData encoding:NSASCIIStringEncoding]);                }  

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.