• 以post方式发送UIImage到服务器


    //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]);	
    }
    
  • 相关阅读:
    实验六:空间耗尽故障
    实验四 :重置root密码
    实验三:误删boot恢复
    实验二: grub引导故障修复
    实验一 :MBR扇区故障系统备份与还原
    chapter07
    chapter06
    chapter05
    转-SQL数据库中把一个表中的数据复制到另一个表中
    Howto: 如何通过IIS7为ArcGIS Server配置反向代理系统架构
  • 原文地址:https://www.cnblogs.com/peer/p/2043894.html
Copyright © 2020-2023  润新知