IOS development tutorial for put upload file server configuration and instance sharing

  • 2020-05-06 11:42:28
  • OfStack

1, HTTP common method

GET gets the specified resource

POST 2M submits data to a specified resource for processing requests, and in the RESTful style is used to add a new resource HEAD to get 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:


  1>
n  Open a terminal 
p cd /etc/apache2
p sudo vim httpd.conf
n  in vim In the input 
p /httpd-dav.conf
•  To find the httpd-dav.conf
p  According to the 0 Move the cursor to the beginning of the line  p  According to the x The beginning of a line # delete 
p  The input :wq, Save and exit  
  2>
 Continue typing at the terminal 
 cd /etc/apache2/extra
 sudo vim httpd-dav.conf
   in vim Mark the first place on the right in red   the Digest Modified to Basic
   The input :wq, Save and exit 
   prompt :
   What is changed is the way the user is authorized 
   The second place marked in red is to save the user password   The file (/user/user.passwd)
   The third red position is able to   with PUT The requested user name (admin) 
 4>
 Input at terminal  p cd /usr
  sudo htpasswd -c /usr/user.passwd admin 
  ls-l 
 sudo chgrp www /usr/user.passwd 
  ls-l 
  5>
 To establish var folder , save DavLockDB The relevant documents  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
   Restart the Apache
 sudo apachectl -k restart 
   6> When you see this, you're configured correctly 
   Modified with ls -l The schematic view is as follows 
   If you can see these three, you are configured correctly 
      uploads
      user.passwd
      var

Example:


#import "KUViewController.h"
#import "KUProgress.h"
@interfaceKUViewController ()<NSURLSessionTaskDelegate>
// Download the progress of the class, inheritance UIview
@property (weak, nonatomic) IBOutlet  KUProgress *progressView;

@end

@implementation KUViewController

- (void)viewDidLoad
{
    [superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
    [self putFile];
}

/**
 *   with PUT Method to upload a file without passing it through the browser 
 */
-(void)putFile
{
   //1,url( agreement + The host name + The path + File name to save to the server )
     // post:url  ( agreement + The host name + Upload the server program )
    NSString *urlStr = @"http://localhost/uploads/046.Post Submit user privacy data &MD5 encryption .mp4";
      //1.1 Coding 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 At present, one of the most popular encoding methods on the network, can be converted into a binary data string, after the other party received, can be converted into a binary file string 
        BASE64 It can be encoded, it can be decoded 

       Authorization format: 
       ( 1 Authorization string format: username: password 
       ( 2 ) authorization mode: Basic Base64 The encoded authorization string 
       ( 3 ) HTTPHEADERField the Authorization The assignment 

     */
    NSString *authStr = @"admin:admin";
    // Converts a string to  Base64
     authStr = [self authBase64:authStr];
    // Convert to part 2 
    NSString *authBase64 = [NSString stringWithFormat:@"Basic %@",authStr];
    // Switch to part three 
    [request setValue:authBase64 forHTTPHeaderField:@"Authorization"];

    //3 . session
      //1>. Create session mechanism 
    NSURLSessionConfiguration *config = [NSURLSessionConfigurationdefaultSessionConfiguration];
  NSURLSession *session =  [NSURLSessionsessionWithConfiguration:config delegate:selfdelegateQueue:[[NSOperationQueuealloc] init]];

    //2>  Upload task 
    // The path to 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 a way to not download the progress bar.  
//    NSURLSessionUploadTask *task = [session uploadTaskWithRequest:request fromFile:fileUrl completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
//        
//        // Converts binary data into a string 
//      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 ");
}
// Converted to Base64 Encoded authorization string 
-(NSString *)authBase64:(NSString *)authStr
{

    // Converts a string to binary games 
    NSData *data = [authStr dataUsingEncoding:NSUTF8StringEncoding];
    return [data base64EncodedStringWithOptions:0];
}


Related articles: