IOS asi--File Upload

Source: Internet
Author: User

1. Sample code for File upload

//Set URL

Nsurl *url = [Nsurl urlwithstring:@ "Http://localhost/photo"];

//Setup Request

Self.postrequest = [Asiformdatarequest Requestwithurl:url];

//Specify the path of the file to be uploaded

NSString *file = [[NSBundle mainbundle] pathforresource:@ "123.txt" oftype:nil];

//settings and file-related parameters

[self.postrequest setfile:file forkey:@ "file"]; //Method 1, what is the filename of the client local file, and what file name is uploaded to the server side

//[Self.postrequest setfile:file withfilename:@ "NewName.txt" andcontenttype:@ "Text/plain" forkey:@ "file"]; //Method 2, you can customize the file name

//NSData *data = [NSData datawithcontentsoffile:file]; //Method 3, when the path of the local file cannot be obtained, it is appropriate to use this method, such as photo upload

//[self.postrequest setdata:data forkey:@ "file"];

//Set additional request parameters

[Self.postrequest setpostvalue:@ "ios" forkey:@ "username"];

[Self.postrequest setpostvalue:@ "123" forkey:@ "pwd"];

//Receive data returned by the server

[Self.postrequest setcompletionblock:^{

NSLog (@ "upload finished");

}];

//initiating a request

[Self.postrequest startasynchronous];

2. Photo Upload

2.1 First write the photo to the album. Before writing, user authorization is required, and iOS automatically handles authorization; Photos can be seen in the emulator's album after they are written to the album.

UIImage *image = [UIImage imagenamed:@ "123.png"];

Uiimagewritetosavedphotosalbum (image, nil, nil, nil);

2.2 Getting Photos

Open album

-(void) Touchesbegan: (Nsset *) touches withevent: (uievent *) event{

[Self pickphoto];

}

-(void) pickphoto{

Uiimagepickercontroller *pick = [[Uiimagepickercontroller alloc]init];

Pick.sourcetype = uiimagepickercontrollersourcetypephotolibrary; Set the photo source, the 1th and 3rd almost, are the source album; 2nd one is the source camera.

Uiimagepickercontrollersourcetypephotolibrary,

Uiimagepickercontrollersourcetypecamera,

Uiimagepickercontrollersourcetypesavedphotosalbum

Pick.delegate = self; //To be a photo selector agent, you need to comply with 2 protocol uinavigationcontrollerdelegate, Uiimagepickercontrollerdelegate

[Self Presentviewcontroller:pick animated:yes completion:nil];

}

#pragma mark-uiimagepickercontrollerdelegate

-(void) Imagepickercontroller: (Uiimagepickercontroller *) Picker Didfinishpickingmediawithinfo: (NSDictionary *) info {

[Self dismissviewcontrolleranimated:yes completion:nil];

UIImage *image = Info[uiimagepickercontrolleroriginalimage];

[Self upload:image];

}

-(void) Upload: (UIImage *) image{

Set URL

Nsurl *url = [Nsurl urlwithstring:@ "Http://localhost/photo"];

Set Request

Self.postrequest = [Asiformdatarequest Requestwithurl:url];

NSData *data = uiimagepngrepresentation (image);

[Self.postrequest setdata:data withfilename:@ "newimage.png" andcontenttype:@ "image/png" forKey:@ "file"];

Set additional request parameters

[Self.postrequest setpostvalue:@ "ios" forkey:@ "username"];

[Self.postrequest setpostvalue:@ "123" forkey:@ "pwd"];

Receive the data returned by the server

[Self.postrequest setcompletionblock:^{

NSLog (@ "upload finished");

}];

Initiating a request

[Self.postrequest startasynchronous];

}

Note: The above code is to upload photos in the album, if it is to upload the camera has just taken a photo, just need to Pick.sourcetype = Uiimagepickercontrollersourcetypephotolibrary, changed to Pick.sourcetype = Uiimagepickercontrollersourcetypecamera, other code is not changed.

3. Large file upload, pay attention to the large file upload do not use data upload, should be uploaded with file, because the large files serialized into NSData very expensive performance, and not so much memory

Set URL

Nsurl *url = [Nsurl urlwithstring:@ "Http://localhost/bigDataFolder"];

Set Request

Self.postrequest = [Asiformdatarequest Requestwithurl:url];

Suppose the big file is in the cache.

NSString *cache = [Nssearchpathfordirectoriesindomains (nscachesdirectory, Nsuserdomainmask, YES) lastObject];

NSString *filepath = [Cache stringbyappendingpathcomponent:@ "Data.zip"];

[Self.postrequest setfile:filepath forkey:@ "file"];

Set additional request parameters

[Self.postrequest setpostvalue:@ "ios" forkey:@ "username"];

[Self.postrequest setpostvalue:@ "123" forkey:@ "pwd"];

Upload Progress

Self.postRequest.uploadProgressDelegate = Self.progressview;

Listening requests

[Self.postrequest setcompletionblock:^{

NSLog (@ "upload finished");

}];

Initiating a request

[Self.postrequest startasynchronous];

Added: ASI does not support breakpoint uploads, ASI is based on HTTP protocol, while HTTP protocol does not support breakpoint upload, TCP/IP support

IOS asi--File Upload

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.