IOS development-File Upload

Source: Internet
Author: User

IOS development-File Upload
IOS development-File Upload

It is essential to upload files in the form of mobile app development files. Recently, I have simply sorted out the file upload code for IOS files. If you need Android code, I can also share it with you! QQ group: 74432915 everyone is welcome to discuss
First of all, this demo uses the open source framework AFNetworking source code: http://download.csdn.net/detail/wangliang198901/7809439
Import the entire framework to a new IOS Project
The FKAppDelegate. h statement is as follows:

# Import

# Import "AFHTTPRequestOperationManager. h"


@ Interface FKAppDelegate: UIResponder


@ Property (strong, nonatomic) UIWindow * window;

@ Property (strong, nonatomic) AFHTTPRequestOperationManager * manager;

@ End


Then initialize in the FKAppDelegate. m file

# Import "FKAppDelegate. h"


@ Implementation FKAppDelegate


-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions

{

Self. manager = [AFHTTPRequestOperationManagermanager];

Self. manager. responseSerializer = [[AFHTTPResponseSerializeralloc] init];

Return YES;

}



Then, perform the following operations in the Custom ViewController:

# Import "FKViewController. h"

# Import "FKAppDelegate. h"

@ Interface FKViewController ()

{

FKAppDelegate * appDelegate;

NSArray * images;

}

@ End

@ Implementation FKViewController


-(Void) viewDidLoad

{

[SuperviewDidLoad];

AppDelegate = [UIApplicationsharedApplication]. delegate;

Self. picker. dataSource = self;

Self. picker. delegate = self;

// Use the simplified syntax to create an NSArray set

Images = @ [@ "logo", @ "java", @ "android"];

}

// The method defined in UIPickerViewDataSource. the return value of this method determines how many columns the control contains.

-(NSInteger) numberOfComponentsInPickerView :( UIPickerView *) pickerView

{

// If the return value is 1, the control contains only one column.

Return1;

}

-(NSInteger) pickerView :( UIPickerView *) pickerView

NumberOfRowsInComponent :( NSInteger) component

{

Returnimages. count;

}

# Define kImageTag 1

-(UIView *) pickerView :( UIPickerView *) pickerView viewForRow:

(NSInteger) row forComponent :( NSInteger) component

ReusingView :( UIView *) view

{


// If the tag of a reusable view is not kImageTag, it indicates that the view does not exist and needs to be created again.

If (view. tag! = KImageTag)

{

View = [[UIViewalloc] init];

// Set tag attributes for the UIView

View. tag = kImageTag;

// The settings do not allow user interaction

View. userInteractionEnabled = NO;

UIImageView * iv = [[UIImageViewalloc] initWithImage:

[UIImageimageNamed: [imagesobjectAtIndex: row];

Iv. frame = CGRectMake (0, 0, 48, 48 );

Iv. contentMode = UIViewContentModeScaleAspectFit;

[ViewaddSubview: iv];

}

Return view;

}

// The method defined in UIPickerViewDelegate. The returned value of this method determines the height of the list item.

-(CGFloat) pickerView :( UIPickerView *) pickerView

RowHeightForComponent :( NSInteger) component

{

Return48;

}

// The method defined in UIPickerViewDelegate. the return value of this method determines the width of the list item.

-(CGFloat) pickerView :( UIPickerView *) pickerView

WidthForComponent :( NSInteger) component

{

Return48;

}

-(IBAction) upload :( id) sender

{

// Obtain the row selected by the user

NSInteger selectedRow = [self. pickerselectedRowInComponent: 0];

// Obtain the selected file name

NSString * fileName = [imagesobjectAtIndex: selectedRow];

// Determine the file to be uploaded Based on the selected file name

NSURL * filePath = [[NSBundlemainBundle] URLForResource: fileName

WithExtension: @ "png"];

NSDictionary * parameters =@{@ "name": @ "Additional Request parameters "};

// Use AFHTTPRequestOperationManager to send a POST request

[AppDelegate. manager

POST: @ "http: // 192.168.1.88: 8888/AFNetworkingServer/upload"

Parameters: parameters

// Use a code block to encapsulate the file data to be uploaded

ConstructingBodyWithBlock: ^ (id formData)

{

[FormDataappendPartWithFileURL: filePath // specify the uploaded file

Name: @ "file" // specify the name of the Request Parameter corresponding to the file to be uploaded

// Specify the original file name

FileName: [NSStringstringWithFormat: @ "symbol @.png", fileName]

// Specify the MIME type of the uploaded file

MimeType: @ "image/png"

Error: nil];

}

// Obtain the code block triggered when the server response is successful

Success: ^ (AFHTTPRequestOperation * operation, id responseObject)

{

// When the HTTP Response parser is used, the server response data is encapsulated in NSData

// Convert NSData to NSString and use UIAlertView to display the logon result.

[[UIAlertViewalloc] initWithTitle: @ "Upload result" message:

[[NSStringalloc] initWithData: responseObjectencoding:

NSUTF8StringEncoding] delegate: self

CancelButtonTitle: @ "OK" otherButtonTitles: nil]

Show];

}

// Obtain the code block triggered when the server response fails.

Failure: ^ (AFHTTPRequestOperation * operation, NSError * error)

{

NSLog (@ "An error occurred while obtaining server response! ");

}];

}

@ End



Source code download: http://download.csdn.net/detail/wangliang198901/7813361 Note: This article is Personal Original, please respect the results of personal labor, thank you!





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.