#import ""
#import ""
@interfaceKUViewController ()<NSURLSessionTaskDelegate>
//The class of download progress, inherit 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];
}
/**
* Upload files using PUT method without passing them through the browser
*/
-(void)putFile
{
//1,url (protocol + hostname + path + file name saved to server)
// post:url (Protocol + Hostname + Uploaded Server Program)
NSString *urlStr = @"http://localhost/uploads/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
= @"PUT";
//2>Network request authorization
/**
BASE64 is currently the most popular encoding method on the Internet. It can convert binary data into strings. After the other party accepts it, it can then convert the string into 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";
//Convert string to Base64
authStr = [self authBase64:authStr];
//Convert to the second part
NSString *authBase64 = [NSString stringWithFormat:@"Basic %@",authStr];
//Convert to the third part
[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 tasks
//Path of uploaded file
NSURL *fileUrl = [[NSBundle mainBundle] URLForResource:@"Submit user privacy data & MD5 encryption.mp4" withExtension:nil];
[[session uploadTaskWithRequest:request fromFile:fileUrl] resume];
// This is a method without downloading the progress bar.
// NSURLSessionUploadTask *task = [session uploadTaskWithRequest:request fromFile:fileUrl completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
//
// //Convert binary data into 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:^{
= 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
{
//Convert strings to binary numbers
NSData *data = [authStr dataUsingEncoding:NSUTF8StringEncoding];
return [data base64EncodedStringWithOptions:0];
}