前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >POST上传各种数据类型(包括图片)

POST上传各种数据类型(包括图片)

作者头像
用户1451823
发布2018-09-13 16:53:32
2.7K0
发布2018-09-13 16:53:32
举报
文章被收录于专栏:DannyHoo的专栏

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://cloud.tencent.com/developer/article/1338102

1.上传事件:

  • (void)postUpLoad {

// 1. url

NSURL *url = NSURL URLWithString:@"http://127.0.0.1/post/upload.php";

// 2. post请求

/*

参数1:上传到服务器的地址

参数2:上传文件的全路径(可以上传图片,也可以上传其他类型数据,因为最后上传的都是二进制数据流)

参数3:保存到服务器的文件名

     */

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url andLoaclFilePath:[NSBundle mainBundle pathForResource:@"001.png" ofType:nil] andFileName:@"123456.png"];

// 3. 连接

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

// 反序列化处理

id result = NSJSONSerialization JSONObjectWithData:data options:0 error:NULL;

NSLog(@"result = %@", result);

    }];

}

 result);    }];

NSMutalbeURLRequest分类:

/**

 url: 要上传的服务器的地址

 loaclFilePath: 要上传的文件的全路径

 fileName:保存到服务器的文件名

 */

  • (instancetype)requestWithURL:(NSURL *)url andLoaclFilePath:(NSString *)loaclFilePath andFileName:(NSString *)fileName;

方法的实现:

/**随便的字符串作为分隔符*/

static NSString *boundary = @"itcastupload";

@implementation NSMutableURLRequest (Multipart)

  • (instancetype)requestWithURL:(NSURL *)url andLoaclFilePath:(NSString *)loaclFilePath andFileName:(NSString *)fileName

{

// 2. post请求

NSMutableURLRequest *request = NSMutableURLRequest requestWithURL:url cachePolicy:1 timeoutInterval:2.0f;

// 2.1 指定post方法

    request.HTTPMethod = @"POST";

// 2.2 拼接数据体

NSMutableData *dataM = NSMutableData data;

//   1. \r\n--(可以随便写, 但是不能有中文)\r\n

NSString *str = NSString stringWithFormat:@"\r\n--%@\r\n", boundary;

    [dataM appendData:str dataUsingEncoding:NSUTF8StringEncoding];

//   2. Content-Disposition: form-data; name="userfile(php脚本中用来读取文件的字段)"; filename="demo.json(要保存到服务器的文件名)"

    str = NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@\" \r\n", fileName;

    [dataM appendData:str dataUsingEncoding:NSUTF8StringEncoding];

//   3. Content-Type: application/octet-stream(上传文件的类型)\r\n\r\n

    str = @"Content-Type: application/octet-stream\r\n\r\n";

    [dataM appendData:str dataUsingEncoding:NSUTF8StringEncoding];

//   4. 要上传的文件的二进制流

// 要上传图片的二进制

    [dataM appendData:NSData dataWithContentsOfFile:loaclFilePath];

//   5. \r\n--(可以随便写, 但是不能有中文)--\r\n

    str = NSString stringWithFormat:@"\r\n--%@--\r\n", boundary;

    [dataM appendData:str dataUsingEncoding:NSUTF8StringEncoding];

// 2.4 设置请求体

    request.HTTPBody = dataM;

// 设置请求头

//    Content-Length(文件的大小) 290

//    Content-Type multipart/form-data; boundary(分隔符)=(可以随便写, 但是不能有中文)

NSString *headerStr = NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary;

    request setValue:headerStr forHTTPHeaderField:@"Content-Type";

return request;

}

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016年03月17日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档