生活随笔
收集整理的這篇文章主要介紹了
iOS开发-文件上传原理
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
編寫文件上傳類UploadFile.h //
// UploadFile.h
// 02.Post上傳
//
// Created by wyh on 15-1-29.
// Copyright (c) 2015年 itcast. All rights reserved.
//
#import <Foundation/Foundation.h>@interface UploadFile : NSObject- (void)uploadFileWithURL:(NSURL *)url data:(NSData *)data;@end ?
編寫文件上傳類UploadFile.m //
// UploadFile.m
// 02.Post上傳
//
// Created by why on 15-1-29.
// Copyright (c) 2015年 itcast. All rights reserved.
//
#import "UploadFile.h"@implementation UploadFile
// 拼接字符串
static NSString *boundaryStr = @"--"; // 分隔字符串
static NSString *randomIDStr; // 本次上傳標示字符串
static NSString *uploadID; // 上傳(php)腳本中,接收文件字段- (instancetype)init
{self = [super init];if (self) {randomIDStr = @"itcast";uploadID = @"uploadFile";}return self;
}#pragma mark - 私有方法
- (NSString *)topStringWithMimeType:(NSString *)mimeType uploadFile:(NSString *)uploadFile
{NSMutableString *strM = [NSMutableString string];[strM appendFormat:@"%@%@\n", boundaryStr, randomIDStr];[strM appendFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\n", uploadID, uploadFile];[strM appendFormat:@"Content-Type: %@\n\n", mimeType];NSLog(@"%@", strM);return [strM copy];
}- (NSString *)bottomString
{NSMutableString *strM = [NSMutableString string];[strM appendFormat:@"%@%@\n", boundaryStr, randomIDStr];[strM appendString:@"Content-Disposition: form-data; name=\"submit\"\n\n"];[strM appendString:@"Submit\n"];[strM appendFormat:@"%@%@--\n", boundaryStr, randomIDStr];NSLog(@"%@", strM);return [strM copy];
}#pragma mark - 上傳文件
- (void)uploadFileWithURL:(NSURL *)url data:(NSData *)data
{// 1> 數據體NSString *topStr = [self topStringWithMimeType:@"image/png" uploadFile:@"頭像1.png"];NSString *bottomStr = [self bottomString];NSMutableData *dataM = [NSMutableData data];[dataM appendData:[topStr dataUsingEncoding:NSUTF8StringEncoding]];[dataM appendData:data];[dataM appendData:[bottomStr dataUsingEncoding:NSUTF8StringEncoding]];// 1. RequestNSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:0 timeoutInterval:2.0f];// dataM出了作用域就會被釋放,因此不用copyrequest.HTTPBody = dataM;// 2> 設置Request的頭屬性request.HTTPMethod = @"POST";// 3> 設置Content-LengthNSString *strLength = [NSString stringWithFormat:@"%ld", (long)dataM.length];[request setValue:strLength forHTTPHeaderField:@"Content-Length"];// 4> 設置Content-TypeNSString *strContentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", randomIDStr];[request setValue:strContentType forHTTPHeaderField:@"Content-Type"];// 3> 連接服務器發送請求[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];NSLog(@"%@", result);}];
}@end ?
控制器調用 //
// MJViewController.m
// 02.Post上傳
//
// Created by wyh on 15-1-29.
// Copyright (c) 2015年 itcast. All rights reserved.
//
#import "ViewController.h"
#import "UploadFile.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad
{[super viewDidLoad];UploadFile *upload = [[UploadFile alloc] init];NSString *urlString = @"http://localhost/upload.php";NSString *path = [[NSBundle mainBundle] pathForResource:@"頭像1.png" ofType:nil];NSData *data = [NSData dataWithContentsOfFile:path];[upload uploadFileWithURL:[NSURL URLWithString:urlString] data:data];
}@end ?
轉載于:https://www.cnblogs.com/wangyinghui/p/4356719.html
總結
以上是生活随笔為你收集整理的iOS开发-文件上传原理的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。