当前位置:网站首页>AFNetworking框架_上传文件或图像server
AFNetworking框架_上传文件或图像server
2022-07-06 10:19:00 【全栈程序员站长】
大家好,又见面了,我是全栈君。
的文本 XXXXXXXXXX在自己的论点更填写
– (void)uploadImageWithImage:(NSString *)imagePath
{
//上传其它所需參数
NSString *userId=XXXXXXXXXXX;
NSString *token=XXXXXXXXXXX;
//上传请求POST
AFHTTPClient *client=[AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@””]];
NSString *urlString=[NSString stringWithFormat:@”上传server地址];
NSDictionary *dic=[[NSDictionary alloc] initWithObjectsAndKeys:userId,@”XXXXXX”,token,@”XXXXXX”, nil];
NSURLRequest *request = [client multipartFormRequestWithMethod:@”POST” path:urlString parameters:dic constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
//得到须要上传的数据
NSData *data=[NSData dataWithContentsOfFile:imagePath];
//上传时使用当前的系统事件作为文件名称
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @”yyyyMMddHHmmss”;
NSString *str = [formatter stringFromDate:[NSDate date]];
NSString *fileName = [NSString stringWithFormat:@”%@.jpg”, str];
/*
此方法參数
1. 要上传的[二进制数据]
2. 相应站点上[upload.php中]处理文件的[字段”file”]
3. 要保存在server上的[文件名称]
4. 上传文件的[mimeType]
*/
//server上传文件的字段和类型
[formData appendPartWithFileData:data name:@”XXXXX” fileName:fileName mimeType:@”image/jpg/file”];
}];
// 3. operation包装的urlconnetion
AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@”上传完毕”);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@”上传失败->%@”, error);
}];
//运行
[client.operationQueue addOperation:op];
}
版权声明:本文博客原创文章。博客,未经同意,不得转载。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/117393.html原文链接:https://javaforall.cn
边栏推荐
- Recursive way
- Introduction to the usage of model view delegate principal-agent mechanism in QT
- 【Android】Kotlin代码编写规范化文档
- 当保存参数使用结构体时必备的开发技巧方式
- Jerry's setting currently uses the dial. Switch the dial through this function [chapter]
- STM32+ENC28J60+UIP协议栈实现WEB服务器示例
- Recommend easy-to-use backstage management scaffolding, everyone open source
- Is it meaningful for 8-bit MCU to run RTOS?
- J'aimerais dire quelques mots de plus sur ce problème de communication...
- DNS hijacking
猜你喜欢

Olivetin can safely run shell commands on Web pages (Part 1)

TOP命令详解

FMT open source self driving instrument | FMT middleware: a high real-time distributed log module Mlog

I want to say more about this communication failure

STM32按键状态机2——状态简化与增加长按功能

30 minutes to understand PCA principal component analysis
![Jerry's updated equipment resource document [chapter]](/img/6c/17bd69b34c7b1bae32604977f6bc48.jpg)
Jerry's updated equipment resource document [chapter]

【Android】Kotlin代码编写规范化文档

The integrated real-time HTAP database stonedb, how to replace MySQL and achieve nearly a hundredfold performance improvement

Prophet模型的简介以及案例分析
随机推荐
Jerry's access to additional information on the dial [article]
Distill knowledge from the interaction model! China University of science and Technology & meituan proposed virt, which combines the efficiency of the two tower model and the performance of the intera
Dichotomy (integer dichotomy, real dichotomy)
Kill -9 system call used by PID to kill process
Is it meaningful for 8-bit MCU to run RTOS?
STM32+MFRC522完成IC卡号读取、密码修改、数据读写
win10系统下插入U盘有声音提示却不显示盘符
C语言通过指针交换两个数
Pytest learning ----- pytest operation mode and pre post packaging of interface automation testing
Appium automated test scroll and drag_ and_ Drop slides according to element position
C language exchanges two numbers through pointers
Jerry is the custom background specified by the currently used dial enable [chapter]
Fleet tutorial 13 basic introduction to listview's most commonly used scroll controls (tutorial includes source code)
The integrated real-time HTAP database stonedb, how to replace MySQL and achieve nearly a hundredfold performance improvement
2022暑期项目实训(一)
HMS core machine learning service creates a new "sound" state of simultaneous interpreting translation, and AI makes international exchanges smoother
Jerry's watch deletes the existing dial file [chapter]
推荐好用的后台管理脚手架,人人开源
Top command details
MSF horizontal MSF port forwarding + routing table +socks5+proxychains