Home Web Front-end HTML Tutorial 从HTML上传图片到AFNetWorking上传图片代码浅析_html/css_WEB-ITnose

从HTML上传图片到AFNetWorking上传图片代码浅析_html/css_WEB-ITnose

Jun 24, 2016 am 11:18 AM

先介绍一下背景啊,本人是一名从业2.5年+的IOS开发工程师。平时喜欢搞点小研究,技术上虽然跟大牛们差很远,但是个人觉得写点对别人有帮助的文章也不是什么坏事。这篇文章主要是为了一些不了解图片上传的过程的同学们准备的,之前好几个群友都提到了使用AFNetWorking上传图片不了解是什么过程。可能都是从网上Copy过来的代码,所以不是很清楚流程才导致的不知道该在哪里写什么参数。设置什么参数。下面我就跟大家分享一下。使用Web和AFNetWorking的上传过程。两个前台的代码加上一个PHP后台的代码我想大家会足够明白图片的上传流程了。这就是我举两个例子的原因了,对比着看或许更加事半功倍吧。

首先先从Web上传图片开始说起。贴段代码解释一下吧。

<html><head><meta charset="UTF-8"> <title> Upload Picture. </title></head><body><form action="handle.php" name="form" method="post" enctype="multipart/form-data"> <input type="file" name="fileData" /> <input type="submit" name="submit" value="上传" /></form></body></html>
Copy after login

分析一下上面的代码,其实没有什么可以说的懂html的都知道。是一个提交表单。要点:method=”post” :设置HTTP请求方式为POST请求enctype=”multipart/form-data” :这个是一个需要了解的地方multipart/form-data这个值用于支持向服务器发送二进制数据。这个大家是不是看着感觉似曾相识的感觉呢? AFMultipartFormData协议,这个肯定不陌生了吧。其实AFMultipartFormData协议的作用就等价于multipart/form-data这个了。刚好提到AFMultipartFormData这个协议,那么下面我贴上另外的AFNetWorking上传图片的代码吧。大家都知道,由于IOS不能像Web那样通过提交表单来上传数据,那么我们只能通过HTTP请求来提交数据。代码如下

UIImage *image = [UIImage imageNamed:@"测试图片.jpg"]; NSData *data = UIImageJPEGRepresentation(image, 1.0); AFHTTPSessionManager *session = [AFHTTPSessionManager manager]; [session POST:@"图片上传接口" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData){ [formData appendPartWithFileData :data name:@"fileData" fileName:@"图片名称.jpg" mimeType:@"image/jpeg"]; } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nonnull responseObject){ } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error){ }];
Copy after login

到这里大家一定会发现有点神似并且会有一个共同的地方。就是共同都用到了fileData这个参数。没有错,代码先做了个POST请求,然后使用此协议起到了类似Web提交表单中图片的作用。POST:parameters:constructingBodyWithBlock: 此方法为AFNetWorking中自带方法。百度查一下即可。

前面我介绍了那么多前段的操作,下面我们来看下服务器端怎么来接收图片。以PHP后台为例子了。简单的写了个PHP上传图片的后台。

<?php header('Content-Type:text/json; charset=utf-8'); $file = $_FILES['fileData']; $name = $file['name']; $type = strtolower(substr($name,strrpos($name,'.')+1)); $allow_type = array('jpg','jpeg','gif','png'); if(!in_array($type, $allow_type)){ return ; } if(!is_uploaded_file($file['tmp_name'])){ return ; } $upload_path = "./"; if(move_uploaded_file($file['tmp_name'],$upload_path.$file['name'])){ $array = array( 'code' => 'success' ); echo json_encode($array); }else{ $array = array( 'code' => 'fail' ); echo json_encode($array); }?>
Copy after login

大家是不是又发现了什么?$_FILES[‘fileData’]没错,就是这个了用来获取表单中name为fileData的二进制图片数据。获取到这张图片数据之后将图片保存至服务器。至此为图片上传至服务器的全部流程了。

可能写的不是那么好,不是那么有价值。但是个人感觉还是很实用,希望不喜勿喷。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Is HTML easy to learn for beginners? Is HTML easy to learn for beginners? Apr 07, 2025 am 12:11 AM

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

What is an example of a starting tag in HTML? What is an example of a starting tag in HTML? Apr 06, 2025 am 12:04 AM

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Apr 04, 2025 pm 11:54 PM

GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

How to implement adaptive layout of Y-axis position in web annotation? How to implement adaptive layout of Y-axis position in web annotation? Apr 04, 2025 pm 11:30 PM

The Y-axis position adaptive algorithm for web annotation function This article will explore how to implement annotation functions similar to Word documents, especially how to deal with the interval between annotations...

HTML, CSS, and JavaScript: Essential Tools for Web Developers HTML, CSS, and JavaScript: Essential Tools for Web Developers Apr 09, 2025 am 12:12 AM

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? Apr 05, 2025 am 06:15 AM

To achieve the effect of scattering and enlarging the surrounding images after clicking on the image, many web designs need to achieve an interactive effect: click on a certain image to make the surrounding...

See all articles