国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

利用put上傳文件到服務(wù)器

2019-11-14 20:26:51
字體:
供稿:網(wǎng)友

#import "KUViewController.h"

#import "KU

@interfaceKUViewController ()<NSURLsessionTaskDelegate>

//下載進(jìn)度的類,繼承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方法上傳文件,不經(jīng)過瀏覽器傳遞

 */

-(void)putFile

{

   //1,url(協(xié)議+主機(jī)名+路徑+保存到服務(wù)器的文件名)

     // post:url  (協(xié)議+主機(jī)名+上傳的服務(wù)器的程序)

    NSString *urlStr = @"http://localhost/uploads/abc..mp4";

      //1.1編碼格式

    urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSURL *url = [NSURL URLWithString:urlStr];

    

    //2,request 請(qǐng)求(默認(rèn)是get

    NSMutableURLRequest *request = [NSMutableURLRequestrequestWithURL:url];

      //1>httpMethod

    request.HTTPMethod = @"PUT";

      //2>網(wǎng)絡(luò)請(qǐng)求授權(quán)

    /**

        BASE64目前在網(wǎng)絡(luò)上最流行的一種編碼方式,可以將二進(jìn)制的數(shù)據(jù)轉(zhuǎn)換成字符串,對(duì)方接受到之后,可以再講字符串轉(zhuǎn)換成二進(jìn)制文件

        BASE64可以編碼,也可以解碼

     

      授權(quán)格式:

      1)授權(quán)字符串格式:用戶名:口令

      2)授權(quán)模式:Basic Base64編碼的授權(quán)字符串

      3)位HTTPHEADERFieldAuthorization賦值

     

     */

    NSString *authStr = @"admin:admin";

    //將字符串轉(zhuǎn)換成 Base64

     authStr = [self authBase64:authStr];

    //轉(zhuǎn)換成第二部的

    NSString *authBase64 = [NSString stringWithFormat:@"Basic %@",authStr];

    //轉(zhuǎn)換成第三部

    [request setValue:authBase64 forHTTPHeaderField:@"Authorization"];

    

    //3session

      //1>.創(chuàng)建會(huì)話機(jī)制

    NSURLSessionConfiguration *config = [NSURLSessionConfigurationdefaultSessionConfiguration];

  NSURLSession *session =  [NSURLSessionsessionWithConfiguration:config delegate:selfdelegateQueue:[[NSOperationQueuealloc] init]];

    

    //2> 上傳任務(wù)

    //上傳的文件的路徑

    NSURL *fileUrl =   [[NSBundle mainBundle] URLForResource:@"01.Post提交用戶隱私數(shù)據(jù)&md5加密.mp4" withExtension:nil];

    [[session uploadTaskWithRequest:request fromFile:fileUrl] resume];

    

//   這是不用下載進(jìn)度條的方法。 

//    NSURLSessionUploadTask *task = [session uploadTaskWithRequest:request fromFile:fileUrl completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

//        

//        //把二進(jìn)制數(shù)據(jù)轉(zhuǎn)換成字符串

//      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(@"下載進(jìn)度;value = %.03lf",value);

}

 

-(void)URLSession:(NSURLSession *)session didBecomeInvalidWithError:(NSError *)error

{

    NSLog(@"上傳失敗");

}

//轉(zhuǎn)換成Base64編碼授權(quán)字符串

-(NSString *)authBase64:(NSString *)authStr

{

    

    //將字符串轉(zhuǎn)換成二進(jìn)制數(shù)局

    NSData *data = [authStr dataUsingEncoding:NSUTF8StringEncoding];

    return [data base64EncodedStringWithOptions:0];

}


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 孝昌县| 瓮安县| 深泽县| 阳泉市| 驻马店市| 旬邑县| 唐海县| 云和县| 锦州市| 抚顺市| 台州市| 湘潭市| 永福县| 英德市| 林州市| 县级市| 泾源县| 同仁县| 宜宾县| 肥城市| 雷州市| 盖州市| 大丰市| 攀枝花市| 汉川市| 中西区| 永吉县| 寿阳县| 日照市| 平遥县| 五家渠市| 孟村| 金堂县| 南华县| 砀山县| 承德市| 腾冲县| 手游| 双牌县| 平利县| 昌江|