IOS文件上传

本文将详细介绍如何使用Objective-C实现iOS设备上的文件上传功能,通过创建一个名为MJViewController的类,利用UploadFile类进行文件上传操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


IOS文件上传


 

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //  
  2. //  MJViewController.m  
  3. //  02.Post上传  
  4. //  
  5. //  Created by apple on 14-4-29.  
  6. //  Copyright (c) 2014年 itcast. All rights reserved.  
  7. //  
  8.   
  9. #import "MJViewController.h"  
  10. #import "UploadFile.h"  
  11.   
  12. @interface MJViewController ()  
  13.   
  14. @end  
  15.   
  16. @implementation MJViewController  
  17.   
  18. (void)viewDidLoad  
  19.  
  20.     [super viewDidLoad];  
  21.   
  22.     UploadFile *upload [[UploadFile alloc] init];  
  23.       
  24.     NSString *urlString @"http://localhost/upload.php" 
  25.       
  26.     NSString *path [[NSBundle mainBundle] pathForResource:@"头像1.png" ofType:nil];  
  27.     NSData *data [NSData dataWithContentsOfFile:path];  
  28.       
  29.     [upload uploadFileWithURL:[NSURL URLWithString:urlString] data:data];  
  30.  
  31.   
  32. @end  

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //  
  2. //  UploadFile.m  
  3. //  02.Post上传  
  4. //  
  5. //  Created by apple on 14-4-29.  
  6. //  Copyright (c) 2014年 itcast. All rights reserved.  
  7. //  
  8.   
  9. #import "UploadFile.h"  
  10.   
  11. @implementation UploadFile  
  12. // 拼接字符串  
  13. static NSString *boundaryStr @"--"  // 分隔字符串  
  14. static NSString *randomIDStr;           // 本次上传标示字符串  
  15. static NSString *uploadID;              // 上传(php)脚本中,接收文件字段  
  16.   
  17. (instancetype)init  
  18.  
  19.     self [super init];  
  20.     if (self 
  21.         randomIDStr @"itcast" 
  22.         uploadID @"uploadFile" 
  23.      
  24.     return self 
  25.  
  26.   
  27. #pragma mark 私有方法  
  28. (NSString *)topStringWithMimeType:(NSString *)mimeType uploadFile:(NSString *)uploadFile  
  29.  
  30.     NSMutableString *strM [NSMutableString string];  
  31.       
  32.     [strM appendFormat:@"%@%@\n"boundaryStr, randomIDStr];  
  33.     [strM appendFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\n", uploadID, uploadFile];  
  34.     [strM appendFormat:@"Content-Type: %@\n\n", mimeType];  
  35.       
  36.     NSLog(@"%@"strM);  
  37.     return [strM copy];  
  38.  
  39.   
  40. (NSString *)bottomString  
  41.  
  42.     NSMutableString *strM [NSMutableString string];  
  43.       
  44.     [strM appendFormat:@"%@%@\n"boundaryStr, randomIDStr];  
  45.     [strM appendString:@"Content-Disposition: form-data; name=\"submit\"\n\n"];  
  46.     [strM appendString:@"Submit\n"];  
  47.     [strM appendFormat:@"%@%@--\n"boundaryStr, randomIDStr];  
  48.       
  49.     NSLog(@"%@"strM);  
  50.     return [strM copy];  
  51.  
  52.   
  53. #pragma mark 上传文件  
  54. (void)uploadFileWithURL:(NSURL *)url data:(NSData *)data  
  55.  
  56.     // 1> 数据体  
  57.     NSString *topStr [self topStringWithMimeType:@"image/png" uploadFile:@"头像1.png"];  
  58.     NSString *bottomStr [self bottomString];  
  59.       
  60.     NSMutableData *dataM [NSMutableData data];  
  61.     [dataM appendData:[topStr dataUsingEncoding:NSUTF8StringEncoding]];  
  62.     [dataM appendData:data];  
  63.     [dataM appendData:[bottomStr dataUsingEncoding:NSUTF8StringEncoding]];  
  64.       
  65.     // 1. Request  
  66.     NSMutableURLRequest *request [NSMutableURLRequest requestWithURL:url cachePolicy:0 timeoutInterval:2.0f];  
  67.       
  68.     // dataM出了作用域就会被释放,因此不用copy  
  69.     request.HTTPBody dataM;  
  70.       
  71.     // 2> 设置Request的头属性  
  72.     request.HTTPMethod @"POST" 
  73.       
  74.     // 3> 设置Content-Length  
  75.     NSString *strLength [NSString stringWithFormat:@"%ld"(long)dataM.length];  
  76.     [request setValue:strLength forHTTPHeaderField:@"Content-Length"];  
  77.       
  78.     // 4> 设置Content-Type  
  79.     NSString *strContentType [NSString stringWithFormat:@"multipart/form-data; boundary=%@", randomIDStr];  
  80.     [request setValue:strContentType forHTTPHeaderField:@"Content-Type"];  
  81.       
  82.     // 3> 连接服务器发送请求  
  83.     [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError)  
  84.           
  85.         NSString *result [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];  
  86.         NSLog(@"%@"result);  
  87.     }];  
  88.  
  89.   
  90.   
  91.   
  92. @end  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值