IOS開発教程のputアップロードファイルのサーバーの配置と実例を共有する。

4942 ワード

1,HTTPでよくある方法
GET取得指定リソース
POST 2 Mは指定されたリソースにデータを提出して処理要求を行い、RESTfulスタイルではリソースHEADを追加して指定されたリソースヘッダ情報PUTを取得して指定されたリソースを代替する(ブラウザ操作に対応していない)DELETEは指定されたリソースを削除する。
2,サーバーのput要求方式を設定する:

  1>

n
p cd /etc/apache2
p sudo vim httpd.conf

n vim
p /httpd-dav.conf
• httpd-dav.conf
p 0 p x #
p :wq,
  2>


 cd /etc/apache2/extra
 sudo vim httpd-dav.conf

  vim Digest Basic

  :wq,

  :

 

  (/user/user.passwd)

  PUT (admin)

 4>

p cd /usr

  sudo htpasswd -c /usr/user.passwd admin

  ls-l

 sudo chgrp www /usr/user.passwd

  ls-l

  5>
var , DavLockDB n sudo mkdir -p /usr/var
 sudo chown -R www:www /usr/var

  :uploads
 sudo mkdir -p /usr/uploads
 sudo chown -R www:www /usr/uploads

  Apache
 sudo apachectl -k restart

   6>

  ls -l
 
      uploads
      user.passwd
      var

例:

#import "KUViewController.h"
#import "KUProgress.h"
@interfaceKUViewController ()<NSURLSessionTaskDelegate>
// , 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];
}

/**
 *  PUT ,
 */
-(void)putFile
{
   //1,url( + + + )
     // post:url  ( + + )
    NSString *urlStr = @"http://localhost/uploads/046.Post &MD5 .mp4";
      //1.1
    urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:urlStr];

    //2,request ( get)
    NSMutableURLRequest *request = [NSMutableURLRequestrequestWithURL:url];
      //1>httpMethod
    request.HTTPMethod = @"PUT";
      //2>
    /**
        BASE64 , , ,
        BASE64 ,

      :
      (1) : :
      (2) :Basic Base64
      (3) HTTPHEADERField Authorization

     */
    NSString *authStr = @"admin:admin";
    // Base64
     authStr = [self authBase64:authStr];
    //
    NSString *authBase64 = [NSString stringWithFormat:@"Basic %@",authStr];
    //
    [request setValue:authBase64 forHTTPHeaderField:@"Authorization"];

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

    //2>
    //
    NSURL *fileUrl =   [[NSBundle mainBundle] URLForResource:@"01.Post &MD5 .mp4" withExtension:nil];
    [[session uploadTaskWithRequest:request fromFile:fileUrl] resume];

//   。
//    NSURLSessionUploadTask *task = [session uploadTaskWithRequest:request fromFile:fileUrl completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
//       
//        //
//      NSString *str =  [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
//        NSLog(@"str = %@",str);
//    }];
//

}

#pragma mark --

-(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(@" ;value = %.03lf",value);
}

-(void)URLSession:(NSURLSession *)session didBecomeInvalidWithError:(NSError *)error
{
    NSLog(@" ");
}
// Base64
-(NSString *)authBase64:(NSString *)authStr
{

    //
    NSData *data = [authStr dataUsingEncoding:NSUTF8StringEncoding];
    return [data base64EncodedStringWithOptions:0];
}