AFNetworking 文件上传Data,File图片,文件等上传
一: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图片,文件等上传的更多相关文章
- Linux服务器上实现数据库和图片文件的定时备份
一. 1.首先创建一个目录,用于存放备份的数据 2.在该目录下创建两个子目录一个用于存放数据库的信息,一个用于存放图片资源 3.#数据库的备份 执行下面的命令 mysqldump ...
- 前台页面上传data image图片,java后台接收图片保存
最近在项目中有这么一个需求,就是上传一个视频文件,然后要获取视频文件的第一帧图片,这个可以通过canvas获取得到,得到的是一个dataURL,之后还要将这个图片上传到云,这个时候如何操作就不清楚了, ...
- C#实现图片文件到数据流再到图片文件的转换 --转
/----引入必要的命名空间 using System.IO; using System.Drawing.Imaging; //----代码部分----// private byte[] photo; ...
- C#实现图片文件到数据流再到图片文件的转换
//----引入必要的命名空间using System.IO;using System.Drawing.Imaging; //----代码部分----// private byte[] photo;/ ...
- 使用fastDFS上传和下载图片文件
package com.xuecheng.test.fastdfs;import org.csource.common.MyException;import org.csource.fastdfs.* ...
- PHP上传图片时,如何判断上传的文件是否为可用的图片文件
利用getimagesize函数: function isImage($filename){$types = '.gif|.jpeg|.png|.bmp';//定义检查的图片类型if(file_exi ...
- 小程序上传base64的图片,可上传多张
微信小程序上传图片转化为base64格式 clickimage: function(e) { var index = e.currentTarget.dataset.index; var count ...
- express,node.js实现获取本地文件夹下面的全部图片文件
http://www.luyixian.cn/javascript_show_169354.aspx 按照网上的教程试了多次,处理了各种结果后还有报错, 最后的报错是cant find module ...
- txt文件每行内容与图片文件名字组合,输出txt格式
import os dir_list = os.listdir('C:\\Users\\10107472\\Desktop\\practice\\JPEGImages')i=0f1=open('C:\ ...
随机推荐
- [jquery] input值发生变化则触发
$("#a").val("你好").trigger("change"); $("#a").bind("chan ...
- 转:西部数据NAS设备hack
通过该文学习一下常见硬件web漏洞.重点关注一下几个方面: 1.登录验证代码: 2.文件上传代码: 3.system/exec/popen等是否存在注入可能: 4.调用二进制文件: 5.未登陆可以访问 ...
- CF1020B Badge 【模拟链表】
n个点(n<=1000) 接下来n个整数表示ai 第i个数ai表示i到ai有一条边 输出: n个数 表示从第i个点出发,最先被访问两次的点 样例1: 从1 出发,先到达2,2会到达3,3又到达2 ...
- 洛谷P3258松鼠的新家
题目传送门 恩,很明显的一个树剖题,配合树上差分其实也并不难,不过无奈蒟蒻树剖还没那么熟练,而且树上差分也做的少,所以这题愣是做了一中午......唉,果然我还是太菜了.恩,具体做法在代码中解释吧: ...
- 【Bzoj3527】【Luogu3338】[Zjoi2014]力(FFT)
题面 Bzoj Luogu 题解 先来颓柿子 $$ F_i=\sum_{j<i}\frac{q_iq_j}{(i-j)^2}-\sum_{j>i}\frac{q_iq_j}{(i-j)^2 ...
- 【BZOJ 4513】【SDOI 2016】储能表
http://www.lydsy.com/JudgeOnline/problem.php?id=4513 设\(f(i,0/1,0/1,0/1)\)和\(g(i,0/1,0/1,0/1)\)分别表示d ...
- [BZOJ4815][CQOI2017]小Q的表格(莫比乌斯反演)
4815: [Cqoi2017]小Q的表格 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 832 Solved: 342[Submit][Statu ...
- POJ 2348 Euclid's Game 博弈论
http://poj.org/problem?id=2348 顺便说,必应翻译真的好用,比谷歌翻译好用100倍. 很难判断这道题的具体博弈类型. 有两种写法,一种是找规律,一种是推理得到关系后循环(或 ...
- [Codeforces-div.1 494C] Helping People
[Codeforces-div.1 494C] Helping People 试题分析 不难注意到题目所给的性质是一棵树,所以肯定是树形dp. 那么期望没有办法合并,我们还有一种最笨的方法就是求出概率 ...
- vue组件续和前端工程化
1.3 插槽 slot template: ` <button> <slot></slot> </button> ` <my-button> ...