Use the input stream nsinputstream to upload images and files.

Source: Internet
Author: User
Scenario

Sometimes, we need to save the local iPhone resources (images for example) to the corresponding path of the server. Then you need to upload the local image to the server. In this way, you can use nsinputstream + nsurlconnection + nsmutableurlrequest to upload images.

At the beginning of the body, we need to declare some variables in the header file. h. As follows:
NSURLConnection* _aSynConnection;NSInputStream *_inputStreamForFile;NSString *_localFilePath;@property (nonatomic,retain) NSURLConnection* aSynConnection;@property (nonatomic,retain) NSInputStream *inputStreamForFile;@property (nonatomic,retain) NSString *localFilePath;

The following describes how to upload images.

At the beginning, we need to convert the data and slices we need to send to the nsdata type, and then initialize the inputstreamforfile we just declared through the class method: inputstreamwithfileatpath. Then, it is the normalized HTTP-based data upload. The Code is as follows:
-(Void) btnclickaction :( ID) sender {nsurl * serverurl; nsstring * strurl = @ "http://www.xxx.com/fileName.png"; // here the image is used as an example strurl = [strurl encoding: nsutf8stringencoding]; serverurl = [nsurl urlwithstring: strurl]; // initialize the local file path and link it to nsinputstream self. localfilepath = @ "local image path"; self. inputstreamforfile = [nsinputstream inputstreamwithfileatpath: Self. localfilepath]; // upload size nsnumber * contentlength; contentlength = (nsnumber *) [[[nsfilemanager defaultmanager] attributesofitematpath: Self. localfilepath error: NULL] objectforkey: nsfilesize]; nsmutableurlrequest * request; Request = [nsmutableurlrequest requestwithurl: serverurl]; [Request sethttpmethod: @ "put"]; [Request sethttpbodystream: Self. inputstreamforfile]; [Request setvalue: @ "image/PNG" forhttpheaderfield: @ "Content-Type"]; [Request setvalue: [contentlength description] forhttpheaderfield: @ "Content-Length"]; // request self. asynconnection = [nsurlconnection connectionwithrequest: Request delegate: Self];}

Here is the established asynchronous request, so we also need to implement the Protocol method.

-(Void) connection :( nsurlconnection *) connection response :( nsurlresponse *) aresponse {returninfodata = [[nsmutabledata alloc] init]; totalsize = [aresponse Response]; optional * httpresponse; httpresponse = (nshttpurlresponse *) aresponse; If (httpresponse. status Code/100 )! = 2) {nslog (@ "failed to save");} else {nslog (@ "saved successfully ");}}
Some may ask why we need to upload files using the input stream. You can use nsdata to convert base64 for upload. There is a problem with efficiency. If we use stream mode, data is not converted, but other methods need to be converted. Therefore, we use stream mode, in the same network speed, the speed will be faster. Reference: http://blog.sina.com.cn/s/blog_7b9d64af01019qdr.html

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.