iOS development tutorial put upload file server configuration and instance sharing-standby

Source: Internet
Author: User

Thank the great God for sharing

Common methods of 1,http

Get gets the specified resource

POST 2M submits data to the specified resource for processing requests, used in restful style for new resources head gets the specified resource header information
PUT replaces the specified resource (browser operation not supported)
Delete Deletes the specified resource

2. Configure the server's put request mode:

Copy CodeThe code is as follows:
1>

N Open Terminal
P Cd/etc/apache2
P sudo vim httpd.conf

n Input in Vim
P/httpd-dav.conf
• Find httpd-dav.conf
P Press 0 to move the cursor to the beginning of line P Press X to delete the # of the beginning of the line
P Input: Wq, save and exit
2>

Continue typing at the terminal
Cd/etc/apache2/extra
sudo vim httpd-dav.conf

In Vim, change the digest of the first red position in the right image to basic

Input: Wq, save and exit

Tips:

Modification is the way the user authorizes

The second red location is the file that holds the user's password (/USER/USER.PASSWD)

The third red location is the user name (Admin) that can use the put request

4>

In terminal input P CD/USR

sudo htpasswd-c/usr/user.passwd admin

Ls-l

sudo chgrp www/usr/user.passwd

Ls-l

5>
Create Var folder, save davlockdb related files n sudo mkdir-p/usr/var
sudo chown-r Www:www/usr/var

Create an upload folder: Uploads
sudo mkdir-p/usr/uploads
sudo chown-r www:www/usr/uploads

Re-launch Apache
sudo apachectl-k restart

6> when you see this, it means the configuration is correct.

The following changes are viewed with ls-l
If you can see these three, it means the configuration is correct.
Uploads
user.passwd
Var

Instance:

Copy CodeThe code is as follows:
#import "KUViewController.h"
#import "KUProgress.h"
@interfaceKUViewController () <NSURLSessionTaskDelegate>
Download Progress class, Inherit UIView
@property (Weak, nonatomic) Iboutlet kuprogress *progressview;

@end

@implementation Kuviewcontroller

-(void) viewdidload
{
[Superviewdidload];
Additional setup after loading the view, typically from a nib.
[Self putfile];
}

/**
* Upload files using the Put method, without passing through the browser
*/
-(void) putfile
{
1,url (protocol + hostname + path + file name saved to the server)
Post:url (protocol + host name + uploaded server program)
NSString *urlstr = @ "Http://localhost/uploads/046.Post Submit user privacy Data &md5 encryption. mp4";
1.1 Encoding format
URLSTR = [Urlstr stringbyaddingpercentescapesusingencoding:nsutf8stringencoding];
Nsurl *url = [Nsurl urlwithstring:urlstr];

2,request request (default is Get)
Nsmutableurlrequest *request = [Nsmutableurlrequestrequestwithurl:url];
1>httpmethod
Request. HttpMethod = @ "PUT";
2> Network Request Authorization
/**
BASE64 is currently one of the most popular encoding methods on the Web, you can convert binary data into a string, and the other side can then convert the string into a binary file.
BASE64 can be encoded or decoded.

Authorization format:
(1) Authorization string format: User name: Password
(2) Authorization mode: BASIC BASE64 encoded authorization string
(3) Authorization assignment of Bit Httpheaderfield

*/
NSString *authstr = @ "Admin:admin";
Convert a string to Base64
Authstr = [self authbase64:authstr];
Converted to the second part of the
NSString *authbase64 = [NSString stringwithformat:@ "Basic%@", Authstr];
Convert to Part Three
[Request Setvalue:authbase64 forhttpheaderfield:@ "Authorization"];

3,session
1> Creating a session mechanism
Nsurlsessionconfiguration *config = [Nsurlsessionconfigurationdefaultsessionconfiguration];
Nsurlsession *session = [Nsurlsessionsessionwithconfiguration:config delegate:selfdelegatequeue:[[ NSOPERATIONQUEUEALLOC] [init]];

2> Upload Task
The path of the uploaded file
Nsurl *fileurl = [[NSBundle mainbundle] urlforresource:@ "01.Post submit user privacy Data &md5 encryption. mp4" Withextension:nil ";
[[Session Uploadtaskwithrequest:request Fromfile:fileurl] resume];

This is not a way to download the progress bar.
Nsurlsessionuploadtask *task = [Session uploadtaskwithrequest:request fromfile:fileurl completionhandler:^ (NSData * Data, Nsurlresponse *response, Nserror *error) {
//
Converting binary data to strings
NSString *str = [[NSString alloc] Initwithdata:data encoding:nsutf8stringencoding];
NSLog (@ "str =%@", str);
//    }];
//

}

#pragma mark--proxy method

-(void) Urlsession: (Nsurlsession *) session Task: (Nsurlsessiontask *) Task Didsendbodydata: (int64_t) bytessent Totalbytessent: (int64_t) totalbytessent totalbytesexpectedtosend: (int64_t) Totalbytesexpectedtosend
{
CGFloat value = (cgfloat) totalbytessent/totalbytesexpectedtosend;
[Nsthread sleepfortimeinterval:0.2];
[[Nsoperationqueuemainqueue] addoperationwithblock:^{
self.progressView.progress = value;
}];

NSLog (@ "download progress; value =%.03LF", value);
}

-(void) Urlsession: (Nsurlsession *) session Didbecomeinvalidwitherror: (NSERROR *) error
{
NSLog (@ "upload failed");
}
Convert to BASE64 encoded authorization string
-(NSString *) AuthBase64: (NSString *) authstr
{

Converts a string into a binary number office
NSData *data = [Authstr datausingencoding:nsutf8stringencoding];
return [data base64encodedstringwithoptions:0];
}

iOS development tutorial put upload file server configuration and instance sharing-standby

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.