IOS development tutorial on the server configuration and instance sharing of uploaded files _ios

Source: Internet
Author: User
Tags base64 md5 md5 encryption mkdir string format

Common methods of 1,http

Get gets the specified resource

POST 2M submits data to the specified resource for processing requests, and is used in the restful style for the new resource head to get the specified resource header information
Put replace specified resource (no browser action is supported)
Delete Deletes the specified resource

2, configure the server's put request mode:

Copy Code code as follows:

1>

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

N Enter in vim
P/httpd-dav.conf
• Find httpd-dav.conf
P Press 0 to move the cursor to the beginning of the line p press X to remove the beginning of the line
P Input: Wq, save and exit
2>

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

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

Input: Wq, save and exit

Tips:

Modify the way that the user authorizes

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

The third red position is the user name (Admin) that is able to use the put request

4>

In the terminal input P CD/USR

sudo htpasswd-c/usr/user.passwd admin

Ls-l

sudo chgrp www/usr/user.passwd

Ls-l

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

Build Upload folder: Uploads
sudo mkdir-p/usr/uploads
sudo chown-r www:www/usr/uploads

Restart Apache
sudo apachectl-k restart

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

The schematic diagram of the modified Ls-l view is as follows
If you can see these three, it means the configuration is correct.
Uploads
user.passwd
Var

Instance:

Copy Code code as follows:

#import "KUViewController.h"
#import "KUProgress.h"
@interfaceKUViewController () <NSURLSessionTaskDelegate>
Download the progress class, Inherit UIView
@property (Weak, nonatomic) Iboutlet kuprogress *progressview;

@end

@implementation Kuviewcontroller

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

/**
* Upload the file with the put method without passing through the browser
*/
-(void) putfile
{
1,url (protocol + host name + path + file name saved to server)
Post:url (protocol + host name + uploaded server program)
NSString *urlstr = @ "Http://localhost/uploads/046.Post submits 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 the most popular encoding on the network, you can convert binary data to a string, the other received, you can again speak of string conversion to binary files
BASE64 can be encoded or decoded.

Authorization format:
(1) Authorization string format: Username: password
(2) Authorization mode: BASIC BASE64 encoded authorization string
(3) Authorization assignment of Bit Httpheaderfield

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

3,session
1> Create 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 a progress bar.
Nsurlsessionuploadtask *task = [Session uploadtaskwithrequest:request fromfile:fileurl completionhandler:^ (NSData * Data, Nsurlresponse *response, Nserror *error) {
//
Converts binary data to strings
NSString *str = [[NSString alloc] Initwithdata:data encoding:nsutf8stringencoding];
NSLog (@ "str =%@", str);
//    }];
//

}

#pragma mark--agent 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 bureau
NSData *data = [Authstr datausingencoding:nsutf8stringencoding];
return [data base64encodedstringwithoptions:0];
}

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.