首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 平面设计 > 图形图像 >

利用UIImage的NSData下传多表单到指定服务器 NSMutableURLRequest

2012-07-02 
利用UIImage的NSData上传多表单到指定服务器NSMutableURLRequest?再上传图片的过程中,我用了3天时间终于把

利用UIImage的NSData上传多表单到指定服务器 NSMutableURLRequest

?

再上传图片的过程中,我用了3天时间终于把这个问题给解决了。主要是细节上总是出错。像我标示加粗的地方都是很容易出错的地方。要仔细看清楚了!少一个字符都不行!

接口类的 boundry定义

static? NSString *? const BOUNDRY=@"0xKhTmLbOuNdArY";

boundry=BOUNDRY

发送实体类:

?

@implementation SendBinaryImage

//傳送圖片二進制流?

+(BOOL)sendImage:(NSData *)data1 boundry:(NSString *)boundry{

NSURL? *urlstring=@"http://192.168.10.88/test/test.php";//

NSString? *contentType=[NSString? stringWithFormat:@"multipart/form-data;boundary=%@",boundry];

? ? NSMutableURLRequest? *request=[[[NSMutableURLRequest alloc] init] autorelease];

? ? [request? setURL: [NSURL URLWithString:urlstring]];

[request? setHTTPMethod:@"POST"];

[request? addValue:contentType forHTTPHeaderField:@"Content-Type"];

NSMutableData? *body=[NSMutableData? data];

NSMutableString *str=[[NSMutableString alloc] init];

[str appendFormat:@"\r\n\r\n--%@\r\n",boundry];

[str appendFormat:@"Content-Disposition:form-data; name="username"\r\n\r\n"];

[str appendFormat:@"username"];

[str appendFormat:@"\r\n\r\n--%@\r\n",boundry];

[str appendFormat:@"Content-Disposition:form-data; name="passwd"\r\n\r\n"];

[str appendFormat:@"password"];

[body appendData:[str? dataUsingEncoding:NSUTF8StringEncoding]];//application/octet-stream

[body appendData:[[NSString? stringWithFormat:@"\r\n--%@\r\n",boundry] dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[[NSString stringWithFormat:@"Content-Disposition:form-data; name="filedata"; filename="ttt.jpg"\r\nContent-Type:application/octet-stream\r\nContent-Transfer-Encoding: binary\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];?

[body appendData:[NSData? dataWithData:data1]];

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

[str? release];

?

[request setHTTPBody:body];?

NSURLResponse? *response;

NSError *err;

NSData *returnData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];

NSString *returnString=[[NSStringalloc] initWithData:returnData encoding:NSUTF8StringEncoding];

NSLog(returnString);

NSString*content=@"1";

?

? ? BOOL logined = [content intValue] == 1;

return logined;

}

+(NSString*)getBoundry{

return BOUNDRY;

}

@end

热点排行