一:AFNetworking的文件上传:

主要几个以下类似

- (BOOL)appendPartWithFileURL:(NSURL *)fileURL
name:(NSString *)name
error:(NSError * __autoreleasing *)error;

二:主要代码:

    //配置文件上传

    //图片data 上传
//UIImage *upImage = [UIImage imageNamed:@"testImage.png"];
//NSData *imageData = UIImagePNGRepresentation(upImage); //文件file上传,上传mp3音乐文件
//NSString *theUpFilePath = [NSString stringWithFormat:@"%@testMusic.mp3",NSTemporaryDirectory()]; //上传个图片文件;
NSString *theImagePath = [[NSBundle mainBundle] pathForResource:@"testImage" ofType:@"png"]; self.uploadFileClient = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:kCOCOA_FileUPload]];
NSMutableURLRequest *fileUpRequest = [_uploadFileClient multipartFormRequestWithMethod:@"POST" path:@"" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { //[formData appendPartWithFileData:imageData name:@"file" fileName:@"testImage" mimeType:@"image/png"]; //[formData appendPartWithFileURL:[NSURL fileURLWithPath:theUpFilePath isDirectory:NO] name:@"file" fileName:@"testMusic.mp3" mimeType:@"audio/mpeg3" error:nil]; [formData appendPartWithFileURL:[NSURL fileURLWithPath:theImagePath] name:@"file" error:nil]; }]; self.fileUploadOp = [[AFHTTPRequestOperation alloc]initWithRequest:fileUpRequest];

三:文件上传Demo

 #pragma mark 文件上传;

     //文件的 mine_type http://www.iana.org/assignments/media-types/media-types.xhtml

     UIProgressView *uploadFileProgressView = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleDefault];
uploadFileProgressView.center = CGPointMake(self.view.center.x, );
uploadFileProgressView.progress = ;
uploadFileProgressView.progressTintColor = [UIColor blueColor];
uploadFileProgressView.trackTintColor = [UIColor grayColor];
[self.view addSubview:uploadFileProgressView]; //开始
UIButton *startUp = [UIButton buttonWithType:UIButtonTypeRoundedRect];
startUp.frame = CGRectMake(, , , );
[startUp setTitle:@"开始上传" forState:UIControlStateNormal];
[startUp addTarget:self action:@selector(startUploadOP) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:startUp]; //配置文件上传 //图片data 上传
//UIImage *upImage = [UIImage imageNamed:@"testImage.png"];
//NSData *imageData = UIImagePNGRepresentation(upImage); //文件file上传,上传mp3音乐文件
//NSString *theUpFilePath = [NSString stringWithFormat:@"%@testMusic.mp3",NSTemporaryDirectory()]; //上传个图片文件;
NSString *theImagePath = [[NSBundle mainBundle] pathForResource:@"testImage" ofType:@"png"]; self.uploadFileClient = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:kCOCOA_FileUPload]];
NSMutableURLRequest *fileUpRequest = [_uploadFileClient multipartFormRequestWithMethod:@"POST" path:@"" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { //[formData appendPartWithFileData:imageData name:@"file" fileName:@"testImage" mimeType:@"image/png"]; //[formData appendPartWithFileURL:[NSURL fileURLWithPath:theUpFilePath isDirectory:NO] name:@"file" fileName:@"testMusic.mp3" mimeType:@"audio/mpeg3" error:nil]; [formData appendPartWithFileURL:[NSURL fileURLWithPath:theImagePath] name:@"file" error:nil]; }]; self.fileUploadOp = [[AFHTTPRequestOperation alloc]initWithRequest:fileUpRequest]; [_fileUploadOp setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
startUp.enabled = NO;
[startUp setTitle:@"正在上传" forState:UIControlStateNormal];
CGFloat progress = ((float)totalBytesWritten) / totalBytesExpectedToWrite;
[uploadFileProgressView setProgress:progress animated:YES]; }]; [_fileUploadOp setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
startUp.enabled = NO;
[startUp setTitle:@"完成" forState:UIControlStateNormal];
NSLog(@"upload finish ---%@",[[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding]); } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"error %@",error);
}]; } #pragma mark AFNetworking 文件上传 - (void)startUploadOP
{
[_fileUploadOp start];
}

四:关于文件上传服务器,建议使用php,在 windows 上下载个 wamp 一键就配置成了php 服务器

php 文件上传接口:

查看我的文章:http://www.cnblogs.com/cocoajin/p/3491371.html

AFNetworking 文件上传Data,File图片,文件等上传的更多相关文章

  1. Linux服务器上实现数据库和图片文件的定时备份

    一. 1.首先创建一个目录,用于存放备份的数据   2.在该目录下创建两个子目录一个用于存放数据库的信息,一个用于存放图片资源       3.#数据库的备份 执行下面的命令    mysqldump ...

  2. 前台页面上传data image图片,java后台接收图片保存

    最近在项目中有这么一个需求,就是上传一个视频文件,然后要获取视频文件的第一帧图片,这个可以通过canvas获取得到,得到的是一个dataURL,之后还要将这个图片上传到云,这个时候如何操作就不清楚了, ...

  3. C#实现图片文件到数据流再到图片文件的转换 --转

    /----引入必要的命名空间 using System.IO; using System.Drawing.Imaging; //----代码部分----// private byte[] photo; ...

  4. C#实现图片文件到数据流再到图片文件的转换

    //----引入必要的命名空间using System.IO;using System.Drawing.Imaging; //----代码部分----// private byte[] photo;/ ...

  5. 使用fastDFS上传和下载图片文件

    package com.xuecheng.test.fastdfs;import org.csource.common.MyException;import org.csource.fastdfs.* ...

  6. PHP上传图片时,如何判断上传的文件是否为可用的图片文件

    利用getimagesize函数: function isImage($filename){$types = '.gif|.jpeg|.png|.bmp';//定义检查的图片类型if(file_exi ...

  7. 小程序上传base64的图片,可上传多张

    微信小程序上传图片转化为base64格式 clickimage: function(e) { var index = e.currentTarget.dataset.index; var count ...

  8. express,node.js实现获取本地文件夹下面的全部图片文件

    http://www.luyixian.cn/javascript_show_169354.aspx 按照网上的教程试了多次,处理了各种结果后还有报错, 最后的报错是cant find module ...

  9. txt文件每行内容与图片文件名字组合,输出txt格式

    import os dir_list = os.listdir('C:\\Users\\10107472\\Desktop\\practice\\JPEGImages')i=0f1=open('C:\ ...

随机推荐

  1. 运行时候报异常could only be replicated to 0 nodes instead of minReplication (=1). There are 2 datanode(s) running and no node(s) are excluded in this operation.

    运行时候报异常could only be replicated to 0 nodes instead of minReplication (=1).  There are 2 datanode(s) ...

  2. ZSTU OJ 4273 玩具

    枚举,二分,$RMQ$. 肯定是将连续一段中最大值免去花费,枚举起点之后,二分终点即可.可以证明单调性. #include<map> #include<set> #includ ...

  3. Eclipse控制台

    Eclipse中的控制台,是以卡片布局方式来管理的.每运行一个新的程序,创建一个新的控制台,覆盖到原来的控制台之上. 控制台的控制按钮有10个,仅对其中部分作介绍 第1个按钮结束当前程序的运行 第2个 ...

  4. python3-开发进阶补充Django中的文件的上传

    PS:这段时间有点不在状态,刚刚找回那个状态,那么我们继续曾经的梦想 今天我们来补充一下文件的上传的几种方式: 首先我们先补充的一个知识点: 一.请求头ContentType: ContentType ...

  5. svn出现Authorization failed

    进入svn的conf目录下 修改svnserve.conf [general] anon-access = read auth-access = write password-db = passwd ...

  6. redis源码解析之dict数据结构

    dict 是redis中最重要的数据结构,存放结构体redisDb中. typedef struct dict { dictType *type; void *privdata; dictht ht[ ...

  7. FIREDAC用于LINUX报头文件FireDAC.VCLUI.Wait找不到

    FIREDAC用于LINUX报头文件FireDAC.VCLUI.Wait找不到 FIREDAC LINUX下面,此控件 此属性 必须设为CONSOLE.  默认值只能用于WINDOWS操作系统.

  8. 三种常见的部署Kubernetes的方式

    三种常见的部署Kubernetes的方式 嘹亮的小号 Ghostcloud-001工号,资深Docker玩家,分布式系统研发11年. ​关注他   容器技术将应用程序及其依赖关系与操作系统进行分离,不 ...

  9. LaTeX之参考文献的写法

    在编写latex文件时,参考文献是个比较头疼的问题,以前自己写的时候总是用 \begin{thebibliography}\bibitem author,article, year, vol,\end ...

  10. CSS3 @media 查询,根据屏幕screen大小调节前端显示;媒体查询方法的使用

    ------------------- 1.媒体查询方法在 css 里面这样写 -------------------- @media screen and (min-width: 320px) an ...