• 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]);
    
    	
    	
    }
    
  • 相关阅读:
    About Face 摘录
    断言的使用
    C#中值传递和引用传递
    C++技巧之断言Assert
    About Face 一 目标导向设计
    About Face 二 设计行为与形态
    C++中引用传递与指针传递区别
    一个新的时代SoLoMo
    离散数学笔记算法部分
    汪教授的离散数学20110308 谓词与量词2
  • 原文地址:https://www.cnblogs.com/peer/p/2043889.html
Copyright © 2020-2023  润新知