zoukankan      html  css  js  c++  java
  • 转:ios的图片文件上传代码

    转自: https://gist.github.com/igaiga/1354221

    @interface ImageUploader : NSObject {
    NSData *theImage;
    }
    @property (retain) NSData *theImage;
    - (void) syncUpload:(NSData *) uploadImage;
    @end
     
     
    #import "ImageUploader.h"
     
    #define NOTIFY_AND_LEAVE(X) {[self cleanup:X]; return;}
    #define DATA(X) [X dataUsingEncoding:NSUTF8StringEncoding]
     
    // Posting constants
    #define IMAGE_CONTENT @"Content-Disposition: form-data; name="%@"; filename="image.jpg" Content-Type: image/jpeg "
    #define BOUNDARY @"------------0x0x0x0x0x0x0x0x"
     
    @implementation ImageUploader
    @synthesize theImage;
     
     
    - (void) syncUpload:(NSData *) uploadImage
    {
    self.theImage = uploadImage;
    [self main];
    }
     
    - (void) cleanup: (NSString *) output
    {
    self.theImage = nil;
    // NSLog(@"******** %@", output);
    }
     
    - (NSData*)generateFormDataFromPostDictionary:(NSDictionary*)dict
    {
    id boundary = BOUNDARY;
    NSArray* keys = [dict allKeys];
    NSMutableData* result = [NSMutableData data];
     
    for (int i = 0; i < [keys count]; i++)
    {
    id value = [dict valueForKey: [keys objectAtIndex:i]];
    [result appendData:[[NSString stringWithFormat:@"--%@ ", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
     
    if ([value isKindOfClass:[NSData class]])
    {
    // handle image data
    NSString *formstring = [NSString stringWithFormat:IMAGE_CONTENT, [keys objectAtIndex:i]];
    [result appendData: DATA(formstring)];
    [result appendData:value];
    }
     
    NSString *formstring = @" ";
    [result appendData:DATA(formstring)];
    }
     
    NSString *formstring =[NSString stringWithFormat:@"--%@-- ", boundary];
    [result appendData:DATA(formstring)];
    return result;
    }
     
     
    // NSOperationQueueによる非同期化を見据えてmainにしています。
    - (void) main
    {
    if (!self.theImage) {
    NOTIFY_AND_LEAVE(@"Please set image before uploading.");
    }
    NSString* MultiPartContentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", BOUNDARY];
     
    NSMutableDictionary* post_dict = [[NSMutableDictionary alloc] init];
    [post_dict setObject:@"testimage" forKey:@"filename"];
    [post_dict setObject:self.theImage forKey:@"data[User][image]"];
     
    NSData *postData = [self generateFormDataFromPostDictionary:post_dict];
    [post_dict release];
     
    NSString *baseurl = @"https://example.com/api/upload_image";
    NSURL *url = [NSURL URLWithString:baseurl];
    NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
    if (!urlRequest) NOTIFY_AND_LEAVE(@"Error creating the URL Request");
     
    [urlRequest setHTTPMethod: @"POST"];
    [urlRequest setValue:MultiPartContentType forHTTPHeaderField: @"Content-Type"];
    [urlRequest setHTTPBody:postData];
     
    NSError *error;
    NSURLResponse *response;
    NSData* result = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];
    //NSLog(@"***** result =%@", result);
     
    if (!result)
    {
    [self cleanup:[NSString stringWithFormat:@"upload error: %@", [error localizedDescription]]];
    return;
    }
     
    // Return results
    NSString *outstring = [[[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding] autorelease];
    NSLog(@"***** outstring =%@", outstring);
    [self cleanup: outstring];
    }
     
    @end
  • 相关阅读:
    Python的正则表达式
    Python的异常处理
    Python的类和对象
    Python乘法口诀表
    Python的文件操作
    三层架构介绍和MVC设计模型介绍
    spring的组件使用
    IDEA使用maven搭建spring项目
    Java集合——Collection接口
    Java集合——概述
  • 原文地址:https://www.cnblogs.com/jhj117/p/5035283.html
Copyright © 2011-2022 走看看