码迷,mamicode.com
首页 > 移动开发 > 详细

iOS文件上传

时间:2014-12-16 16:56:55      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:http   ar   io   os   使用   sp   for   strong   on   

iOS文件上传

在ios里来上传文件的方法很多,比如把文件base64直接post,本来打算这样做的,发现各种不爽,服务端收起来也各种不爽,所以干脆用标准的http上传协议mutipart来搞个上传就好,具体代码如下

头文件

1
2
3
4
5
6
7
8
9
10
11
@interface HttpRequest : NSObject 
   
+(id)upload:(NSString *)url widthParams:(NSDictionary *)params; 
   
@end 
   
@interface FileDetail : NSObject 
@property(strong,nonatomic) NSString *name; 
@property(strong,nonatomic) NSData *data; 
+(FileDetail *)fileWithName:(NSString *)name data:(NSData *)data; 
@end

main文件
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// 
//  HttpRequest.m 
//  recofm 
// 
//  Created by reco on 13-12-25. 
//  Copyright (c) 2013年 reco. All rights reserved. 
// 
   
#import "HttpRequest.h" 
   
   
#define BOUNDARY @"----------cH2gL6ei4Ef1KM7cH2KM7ae0ei4gL6" 
   
@implementation HttpRequest 
   
+(id)upload:(NSString *)url widthParams:(NSDictionary *)params { 
       
    NSError *err = nil; 
    NSMutableURLRequest *myRequest = [ [NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString:url] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:0]; 
    [myRequest setHTTPMethod:@"POST"]; 
    [myRequest setValue:[@"multipart/form-data; boundary=" stringByAppendingString:BOUNDARY] forHTTPHeaderField:@"Content-Type"]; 
       
    NSMutableData *body = [NSMutableData data]; 
       
    for(NSString *key in params) { 
        id content = [params objectForKey:key]; 
        if ([content isKindOfClass:[NSString class]] || [content isKindOfClass:[NSNumber class]]) { 
            NSString *param = [NSString stringWithFormat:@"--%@\r\nContent-Disposition: form-data; name=\"%@\"\r\n\r\n%@\r\n",BOUNDARY,key,content,nil]; 
            [body appendData:[param dataUsingEncoding:NSUTF8StringEncoding]]; 
               
        else if([content isKindOfClass:[FileDetail class]]) { 
               
            FileDetail *file = (FileDetail *)content; 
               
            NSString *param = [NSString stringWithFormat:@"--%@\r\nContent-Disposition: form-data; name=\"%@\";filename=\"%@\"\r\nContent-Type: application/octet-stream\r\n\r\n",BOUNDARY,key,file.name,nil]; 
            [body appendData:[param dataUsingEncoding:NSUTF8StringEncoding]]; 
            [body appendData:file.data]; 
            [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
        
    
    NSString *endString = [NSString stringWithFormat:@"--%@--",BOUNDARY]; 
    [body appendData:[endString dataUsingEncoding:NSUTF8StringEncoding]]; 
    [myRequest setHTTPBody:body]; 
       
    NSData *returnData = [NSURLConnection sendSynchronousRequest:myRequest returningResponse:nil error:&err]; 
       
       
       
       
    id jsonObj = [NSJSONSerialization JSONObjectWithData:returnData options:noErr error:nil]; 
       
    return jsonObj; 
   
   
   
   
@end 
   
   
@implementation FileDetail 
+(FileDetail *)fileWithName:(NSString *)name data:(NSData *)data { 
    FileDetail *file = [[self alloc] init]; 
    file.name = name; 
    file.data = data; 
    return file; 
@end

使用方法
1
2
3
4
5
6
7
8
NSData *imageData = UIImageJPEGRepresentation(image,0.8); 
   FileDetail *file = [FileDetail fileWithName:@"avatar.jpg" data:imageData]; 
   NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys: 
                           file,@"file"
                           @"upload",@"action"
                           nil nil]; 
   NSDictionary *result = [HttpRequest upload:@"http://upload.corp.reco.cn/uploadimage" widthParams:params]; 
   NSLog(@"%@",result);

iOS文件上传

标签:http   ar   io   os   使用   sp   for   strong   on   

原文地址:http://www.cnblogs.com/idLuffy/p/4167302.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!