IosASIFormDataRequest upload images to the php server

Source: Internet
Author: User
Tags php file upload
For shoes requiring a third-party library of ASIHTTPRequest, please download my resources. To use the third-party library of ASIHTTPRequest, You need to import the system package as follows: libz.1.2.5.dylib, MobileCoreServices. framework, SystemConfiguration. framework, CFNetwork. framework, CoreGraphics. framework needs to import ASIHTT

For shoes requiring a third-party library of ASIHTTPRequest, please download my resources. To use the third-party library of ASIHTTPRequest, You need to import the system package as follows: libz.1.2.5.dylib, MobileCoreServices. framework, SystemConfiguration. framework, CFNetwork. framework, CoreGraphics. framework needs to import ASIHTT

For shoes that require a third-party ASIHTTPRequest library, please download them from my resources.



To use the ASIHTTPRequest third-party library, you need to import the system package as follows:


Libz.1.2.5.dylib,

MobileCoreServices. framework,

SystemConfiguration. framework,

CFNetwork. framework,

CoreGraphics. framework


Import ASIFormDataRequest. h In the ASIHTTPRequest package

# Import "ASIFormDataRequest. h"


// Upload an image

-(Void) uploadImages :( UIButton *) sender

{

NSData * data = UIImagePNGRepresentation (self. img. image );

NSURL * url = [[NSURLalloc] initWithString: @ "http: // 127.0.0.1/uploadFile/upload. php"];

// Request object in table form

ASIFormDataRequest * request = [[ASIFormDataRequestalloc] initWithURL: url];

Request. delegate = self;

Request. requestMethod = @ "POST"; // you can specify the request method.

// Add request content

[Request addData: datawithFileName: [NSStringstringWithFormat: @ "mongod.png", arc4random ()] andContentType: @ "image/png" forKey: @ "file"];

// Start asynchronous request

[Request startAsynchronous];

// If successful, the task is automatically executed.

[RequestsetDidFinishSelector: @ selector (requestedSuccessfully)];

// Automatically execute the command if it fails.

[RequestsetDidFailSelector: @ selector (requestedFail)];

}

-(Void) requestedSuccessfully

{

UIAlertView * alert = [[UIAlertViewalloc] initWithTitle: @ "prompt" message: @ "image uploaded successfully! "Delegate: nilcancelButtonTitle: @" OK "otherButtonTitles: nil];

[Alertshow];

}

-(Void) requestedFail

{

UIAlertView * alert = [[UIAlertViewalloc] initWithTitle: @ "prompt" message: @ "Image Upload Failed! "Delegate: nilcancelButtonTitle: @" OK "otherButtonTitles: nil];

[Alertshow];

}




Use the php File upload. php

Header ("Content-type: text/html; charset = UTF-8 ");
Print_r ($ _ FILES ['file']);


$ Filename = $ _ FILES ['file'] ['name'];


If (! $ _ FILES ['file'] ['error']) {

If (move_uploaded_file ($ _ FILES ['file'] ['tmp _ name'], "./upload/". $ filename ))
{

Echo "File Uploaded successfully ";

} Else {
Echo "File Upload Failed le ";
}


} Else {

Echo "File Upload error ";
}


?>

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.