iOS development-afnetworking parameters and multiple files simultaneously upload "multi-File Upload"

Source: Internet
Author: User
Tags erro

1. Preface

in the project development, we often need to upload files, such as: Upload images, upload various files, and sometimes need to upload parameters and several files together, do not know that the project encountered no, I in the recent project, I need such a function: Upload parameters, multiple pictures and audio;

2. Function realization

Before implementing this feature, we need to introduce a third-party afnetworking, which is a very powerful network development tool, here I will not say, here is I use this tool, encapsulates a simultaneous upload of parameters and multiple files method:

2.1 Method Name

/**

Multi-file upload, support simultaneous uploading of parameters, multiple images, multiple audio, " you need to convert each file into a date stream, use an array to pass through "

URL: The requested network address

Pram: Request Parameter "I usually use dictionary"

ARRAYIMG: An array of date types for pictures

Arrayaudio: An array of date types for audio

Success: Upload Successful callback

Faile: Upload failed callback

Progress: Upload Progress callback

*/

+ (void) Uploadmorefilehttprequesturl: (nsstring *) URL Requestpram: (ID) Pram arrayimg: (Nsarray *) arrayimg Arrayaudio: ( Nsarray *) Arrayaudio requestsuccess: (void (^) (id respoes)) Success Requestfaile: (void (^) (nserror *erro)) Faile Uploadprogress: (void (^) (nsprogress * uploadprogress)) Progress;

2.2 Method implementation

+ (void) Uploadmorefilehttprequesturl: (nsstring *) URL Requestpram: (ID) Pram arrayimg: (Nsarray *) arrayimg Arrayaudio: ( Nsarray *) Arrayaudio requestsuccess: (void (^) (id respoes)) Success Requestfaile: (void (^) (nserror *erro)) Faile Uploadprogress: (void (^) (nsprogress * uploadprogress)) progress{

Afhttpsessionmanager *manager = [Afhttpsessionmanager manager];

Manager.requestserializer = [Afhttprequestserializer serializer];

Manager.responseserializer = [Afhttpresponseserializer serializer];

[Manager Post:url Parameters:pram constructingbodywithblock:^ (id<afmultipartformdata> _Nonnull formData) {

Set the file name with time

NSDate *date = [NSDate Date];

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

[Formatter setdateformat:@ "YYYYMMDDHHMMSS"];

NSString *datenow = [Formatter stringfromdate:date];

NSString *imgfileid = @ "Handsomekkimg";

NSString *avdiofileid = @ "Ebookavdio";

Image file Data append

for (int i = 0; i < Arrayimg.count; i++) {

File name: This is a different file name, more so I use I to implement

NSString *filename = [NSString stringwithformat:@ "%@%@%d.png", imgfileid,datenow,i];

Image support Type Jpg/png/jpeg

[FormData appendpartwithfiledata:arrayimg[i] name:[nsstring stringwithformat:@ "%@%d", Imgfileid,i] FileName: FileName mimetype:@ "Jpg/png/jpeg"];

}

Here is the audio append and the image append is the same, not the multiple description

for (int i = 0; i < Arrayaudio.count; i++) {

NSString *filename = [NSString stringwithformat:@ "%@%@%d.mp3", avdiofileid,datenow,i];

[FormData appendpartwithfiledata:arrayaudio[i] name:[nsstring stringwithformat:@ "%@%d", Avdiofileid,i] FileNa Me:filename mimetype:@ "MP3"];

//      }

} progress:^ (Nsprogress * _nonnull uploadprogress) {

Progress Callback

Progress (uploadprogress);

} success:^ (Nsurlsessiondatatask * _nonnull task, id _nullable responseobject) {

Upload function callback

if (success) {

Success (Responseobject);

}

} failure:^ (Nsurlsessiondatatask * _nullable task, Nserror * _nonnull error) {

Failure callback

Faile (Error);

}];

}

method, we do not require the file name of the upload fixed, more with the file name is not the same, the background to get out, you according to their own needs, the definition of the file name upload can be.

iOS development-afnetworking parameters and multiple files simultaneously upload "multi-File Upload"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.