ios下使用AFNetworking通过Nginx服务器实现大文件上传
2015-09-13 20:03
946 查看
服务器配置:
服务器环境为:Nginx(1.3.8) + upload module
客户端实现代码:
NSURL *url= [NSURL URLWithString:[NSString stringWithFormat:@"%@?json=%@",nginxserverUrl, jsonUrlStr]];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
manager.requestSerializer.timeoutInterval = 60;
NSURL *filePath = [NSURL fileURLWithPath:@"文件路径"];
[manager POST:[NSString stringWithFormat:@"%@",url] parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileURL:filePath name:@"attach" error:nil];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *responseStr = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"Success: %@", responseStr);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"sendFileToServer -> error = %@", [error userInfo]);
}];
更多IOS开发相关知识,请访问http://www.iosask.com
服务器环境为:Nginx(1.3.8) + upload module
nginx.conf 配置
server { client_max_body_size 100m; listen 80; # Upload form should be submitted to this location location /upload { # Pass altered request body to this location upload_pass @test; # Store files to this directory # The directory is hashed, subdirectories 0 1 2 3 4 5 6 7 8 9 should exist upload_store /tmp 1; # Allow uploaded files to be read only by user upload_store_access user:r; # Set specified fields in request body upload_set_form_field $upload_field_name.name "$upload_file_name"; upload_set_form_field $upload_field_name.content_type "$upload_content_type"; upload_set_form_field $upload_field_name.path "$upload_tmp_path"; # Inform backend about hash and size of a file upload_aggregate_form_field "$upload_field_name.md5" "$upload_file_md5"; upload_aggregate_form_field "$upload_field_name.size" "$upload_file_size"; upload_pass_form_field "^submit$|^description$"; upload_cleanup 400 404 499 500-505; } # Pass altered request body to a backend location @test { proxy_pass http://localhost:8080; } }
客户端实现代码:
NSURL *url= [NSURL URLWithString:[NSString stringWithFormat:@"%@?json=%@",nginxserverUrl, jsonUrlStr]];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
manager.requestSerializer.timeoutInterval = 60;
NSURL *filePath = [NSURL fileURLWithPath:@"文件路径"];
[manager POST:[NSString stringWithFormat:@"%@",url] parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileURL:filePath name:@"attach" error:nil];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *responseStr = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"Success: %@", responseStr);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"sendFileToServer -> error = %@", [error userInfo]);
}];
更多IOS开发相关知识,请访问http://www.iosask.com
相关文章推荐
- 2.4-nginx禁止指定user_agent
- nginx动静分离
- LNMP设置Nginx 404错误页教程[图解]
- 服务器环境配置nginx / php / php-fpm(二)
- nginx 1.80 编译参数及需要的库
- Nginx基本配置、性能优化指南
- Nginx安装部署
- Nginx学习笔记一_UNIX中的IO模型
- Nginx Location配置总结
- 深入了解Nginx之Nginx与Python(1)
- 2.3-nginx访问控制
- 2.2-nginx配置防盗链
- 【云服务器CentOS7使用】 安装Nginx并自动启动
- 2.1-nginx配置静态文件过期时间
- 2.0-nginx日志切割
- nginx 与 lua 开发环境搭建
- 系统测试 - Nginx
- 服务器环境配置nginx / php / php-fpm(一)
- Nginx配置文件nginx.conf中文详解
- 【运维分享】软负载与nginx那些强大的不可不说的功能