iOS開發-檔案上傳原理

來源:互聯網
上載者:User

標籤:

  • 檔案上傳
  1. 編寫檔案上傳類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

     

  2. 編寫檔案上傳類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. Request    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:0 timeoutInterval:2.0f];        // dataM出了範圍就會被釋放,因此不用copy    request.HTTPBody = dataM;        // 2> 設定Request的頭屬性    request.HTTPMethod = @"POST";        // 3> 設定Content-Length    NSString *strLength = [NSString stringWithFormat:@"%ld", (long)dataM.length];    [request setValue:strLength forHTTPHeaderField:@"Content-Length"];        // 4> 設定Content-Type    NSString *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

     

  3. 控制器調用
    ////  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

     

iOS開發-檔案上傳原理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.