**************

#import "HMViewController.h"
#import "AFNetworking.h" @interface HMViewController () <UINavigationControllerDelegate, UIImagePickerControllerDelegate,UIActionSheetDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
- (IBAction)upload;
@end @implementation HMViewController - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
//底部出现弹框
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"请选择图片" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照", @"相册", nil];
[sheet showInView:self.view.window];
} #pragma mark - UIActionSheet
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
// 设置代理
ipc.delegate = self; switch (buttonIndex) {
case : { // 拍照
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) return;
ipc.sourceType = UIImagePickerControllerSourceTypeCamera;
break;
}
case : { // 相册
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) return;
ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
break;
}
default:
break;
} // 显示控制器
[self presentViewController:ipc animated:YES completion:nil];
} #pragma mark - UIImagePickerControllerDelegate
/**
* 在选择完图片后调用
*
* @param info 里面包含了图片信息
*/
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// 销毁控制器
[picker dismissViewControllerAnimated:YES completion:nil]; // 获得图片
UIImage *image = info[UIImagePickerControllerOriginalImage]; // 显示图片
self.imageView.image = image;
} - (void)upload1
{
// 1.创建一个管理者
AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager]; // 2.封装参数(这个字典只能放非文件参数)
NSMutableDictionary *params = [NSMutableDictionary dictionary];
params[@"username"] = @"";
params[@"age"] = @;
params[@"pwd"] = @"";
params[@"height"] = @1.55; // 2.发送一个请求
NSString *url = @"http://localhost:8080/MJServer/upload";
[mgr POST:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
// 在发送请求之前会自动调用这个block
// 需要在这个block中添加文件参数到formData中 /**
FileURL : 需要上传的文件的URL路径
name : 服务器那边接收文件用的参数名
fileName : (告诉服务器)所上传文件的文件名
mimeType : 所上传文件的文件类型
*/
NSURL *url = [[NSBundle mainBundle] URLForResource:@"itcast" withExtension:@"txt"];
[formData appendPartWithFileURL:url name:@"file" fileName:@"test.txt" mimeType:@"text/plain" error:nil]; /**
FileData : 需要上传的文件的具体数据
name : 服务器那边接收文件用的参数名
fileName : (告诉服务器)所上传文件的文件名
mimeType : 所上传文件的文件类型
*/
// UIImage *image = [UIImage imageNamed:@"minion_01"];
// NSData *fileData = UIImagePNGRepresentation(image);
// [formData appendPartWithFileData:fileData name:@"file" fileName:@"haha.png" mimeType:@"image/png"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"上传成功");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"上传失败");
}];
} //点击上传的按钮
- (IBAction)upload {
if (self.imageView.image == nil) return; // 1.创建一个管理者
AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager]; // 2.封装参数(这个字典只能放非文件参数)
NSMutableDictionary *params = [NSMutableDictionary dictionary];
params[@"username"] = @"";
params[@"age"] = @;
params[@"pwd"] = @"";
params[@"height"] = @1.55; // 2.发送一个请求
NSString *url = @"http://192.168.15.172:8080/MJServer/upload";
[mgr POST:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
NSData *fileData = UIImageJPEGRepresentation(self.imageView.image, 1.0);
[formData appendPartWithFileData:fileData name:@"file" fileName:@"haha.jpg" mimeType:@"image/jpeg"]; // 不是用这个方法来设置文件参数
// [formData appendPartWithFormData:fileData name:@"file"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"上传成功");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"上传失败");
}]; // 文件下载,文件比较大,断点续传技术:普遍所有的HTTP服务器都支持
// 文件上传,文件比较大,断点续传技术:一般的HTTP服务器都不支持,常用的技术用的是Socket(TCP\IP、UDP)
}
@end

IOS网络第五天 AFN-02-文件上传,底部弹出窗体,拍照和相册获取图片上传的更多相关文章

  1. iOS 使用AFN 进行单图和多图上传 摄像头/相册获取图片,压缩图片

    图片上传时必要将图片进行压缩,不然会上传失败 首先是同系统相册选择图片和视频.iOS系统自带有UIImagePickerController,可以选择或拍摄图片视频,但是最大的问题是只支持单选,由于项 ...

  2. ios中摄像头/相册获取图片,压缩图片,上传服务器方法总结

    相册 iphone的相册包含摄像头胶卷+用户计算机同步的部分照片.用户可以通过UIImagePickerController类提供的交互对话框来从相册中选择图像.但是,注意:相册中的图片机器路径无法直 ...

  3. ios中摄像头/相册获取图片压缩图片上传服务器方法总结

    本文章介绍了关于ios中摄像头/相册获取图片,压缩图片,上传服务器方法总结,有需要了解的同学可以参考一下下.     这几天在搞iphone上面一个应用的开发,里面有需要摄像头/相册编程和图片上传的问 ...

  4. java模拟表单上传文件,java通过模拟post方式提交表单实现图片上传功能实例

    java模拟表单上传文件,java通过模拟post方式提交表单实现图片上传功能实例HttpClient 测试类,提供get post方法实例 package com.zdz.httpclient; i ...

  5. WPF 选择电脑文件显示路径,弹出资源管理器,打开文件

    选择文件,将路径显示在名为txbx的textbox上 // 在WPF中, OpenFileDialog位于Microsoft.Win32名称空间 Microsoft.Win32.OpenFileDia ...

  6. go语言使用go-sciter创建桌面应用(七) view对象常用方法,文件选择,窗口弹出,请求

    view对象的详细文档请看: https://sciter.com/docs/content/sciter/View.htm demo9.html代码如下: <!DOCTYPE html> ...

  7. Java实现文件上传-按钮弹出上传页面

    转自: https://blessht.iteye.com/blog/1405057 最近自己在做一个小系统玩的时候涉及到了文件的上传,于是在网上找到Java上传文件的方案,最后确定使用common- ...

  8. 扫描仪扫描文件处理-Photoshop批处理弹出色阶设置框解决

    为什么我录制动作明明设置的有色阶,最后批处理的时候仍然弹出了色阶设置框?   出现问题原因可能是你在录入设置色阶动作的时候,是彩色图片或者灰阶中的一种,而批处理的时候遇到了另外一种色彩模式.所以动作中 ...

  9. ios开发网络学习五:输出流以及文件上传

    一:输出流 #import "ViewController.h" @interface ViewController ()<NSURLConnectionDataDelega ...

随机推荐

  1. Linux/CentOS下开启MySQL远程连接,远程管理数据库

    当服务器没有运行PHP.没装phpMyAdmin的时候,远程管理MySQL就显得有必要了. 第一步:开启MySQL用户的远程访问权限 mysql -u root -p mysql # 第1个mysql ...

  2. HDU 5023 A Corrupt Mayor's Performance Art 线段树区间更新+状态压缩

    Link:  http://acm.hdu.edu.cn/showproblem.php?pid=5023 #include <cstdio> #include <cstring&g ...

  3. 如何用angularjs给从后台传来数据添加链接

    <!DOCTYPE html> <html ng-app="myApp"> <head> <meta charset="UTF- ...

  4. awk 双引号vs单引号

    centos下面, awk '{...}' 和 awk "{...}" 差别是很大的: [ywt@YuWentao]$ echo "a b c d 1 2 3 4&quo ...

  5. debug [LTS]

    0613 A. 复制代码的时候忘了后续的对称的修改. 统计答案时出现了一些不可理喻的低级失误. B. 在0-indexed的程序中访问第一个元素使用了Arr[1]. Matrix-tree为mat[d ...

  6. JavaScript高级程序设计学习笔记--BOM

    window对象 BOM的核心对象是window,它表示浏览器的一个实例.在浏览器中,window对象有双重角色,它既是通过JavaScript访问浏览器窗口的一个接口,又是ECMScript规定的G ...

  7. jQuery Mobile学习笔记

    1.获取jQuery mobile 文件,访问jQuerymobile网站下载 (貌似使用jquery mobile后,jquery会自动在网页中添加一些class类,第一次知道的我是被吓呆的!!) ...

  8. C语言字符串操作总结大全

    1)字符串操作 strcpy(p, p1)  复制字符串  函数原型strncpy(p, p1, n)   复制指定长度字符串  函数原型strcat(p, p1)   附加字符串  函数原型strn ...

  9. winform 多个label绑定一个事件

    1当多个label帮到到一个事件后 private void jiandao_Click(object sender, EventArgs e) { //sender显示的是窗体上接受事件的label ...

  10. LeetCode——Best Time to Buy and Sell Stock III (股票买卖时机问题3)

    问题: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...