Post方式发送UIImage数据到服务器

长平狐 发布于 2012/08/13 15:44
阅读 229
收藏 0

//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]);

	
	
}


原文链接:http://blog.csdn.net/favormm/article/details/6081082
加载中
返回顶部
顶部