ios 多文件上传

/**

*  上传多个文件

*

*  @param url      请求接口地址

*  @param filedata 文件名称和数据(key:value)

*  @param btnName  上传网页的btn (接口)

*  @param parms    附加上传参数

*/

-(void)httpSendDataWithUrl:(NSString*)url data:(NSDictionary*)filedata name:(NSString*)btnName parms:(NSDictionary*)parms

{

NSString *httpUrl=url;

//----------------------Post方法的URL和请求头-----------

NSMutableURLRequest *mRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:httpUrl]];

mRequest.HTTPMethod=@"post";

//Content-Type:multipart/form-data; boundary=----WebKitFormBoundarywrkpby4PbAPpMcFc

NSString *str=[NSString stringWithFormat:@"multipart/form-data; boundary=%@",KEYWard];

[mRequest setValue:str forHTTPHeaderField :@"Content-Type"];

mRequest.HTTPBody=[self makeBodydata:filedata name:btnName parms:parms];

//----------------------Post方法的URL和请求头-----------

[NSURLConnection sendAsynchronousRequest:mRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

if (!connectionError) {

NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;

if (httpResponse.statusCode == 200) {

NSError *error=nil;

id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];

if (error) {

NSLog(@"json解析错误!!!!!!");

}

else

{

NSLog(@"%@",json);

}

}else{

NSLog(@"服务器内部错误");

}

}else{

NSLog(@"请求错误%@",connectionError);

}

}];

}

-(NSData*)makeBodydata:(NSDictionary*)filedata name:(NSString*)btnName parms:(NSDictionary*)parms

{

NSMutableData *mData = [NSMutableData data];

//准备文件

[filedata enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSData *data, BOOL *stop) {

//        ------WebKitFormBoundarycwuJbGNWchuHGPRT

//        Content-Disposition: form-data; name="userfile[]"; filename="Snip20150701_10.png"

//        Content-Type: image/png

//1

NSMutableString *mStr = [NSMutableString string];

[mStr appendFormat:@"\r\n--%@\r\n",KEYWard];

[mStr appendFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n",btnName,key];

[mStr appendString:@"application/octet-stream\r\n"];

[mStr appendString:@"\r\n"];

[mData appendData:[mStr dataUsingEncoding:NSUTF8StringEncoding]];

//2

[mData appendData:data];

}];

//准备表单中的数据

[parms enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *value, BOOL *stop) {

//        ------WebKitFormBoundarycwuJbGNWchuHGPRT

//        Content-Disposition: form-data; name="username"

//

//        345345

NSMutableString *mStr = [NSMutableString string];

[mStr appendFormat:@"\r\n--%@\r\n",KEYWard];

[mStr appendFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n",key];

[mStr appendString:@"\r\n"];

[mStr appendFormat:@"%@\r\n",value];

[mData appendData:[mStr dataUsingEncoding:NSUTF8StringEncoding]];

}];

NSString *end = [NSString stringWithFormat:@"--%@--\r\n",KEYWard];

[mData appendData:[end dataUsingEncoding:NSUTF8StringEncoding]];

return mData.copy;

}

上一篇:Hbase过滤器Filter的使用心得(爬坑经验)


下一篇:JS之Callback function(回调函数)