如何在不使用ASIHTTPRequest的情况下上传多个图像

时间:2012-02-03 04:35:05

标签: objective-c ios cocoa-touch

我想上传两张图片到服务器,下面的代码可以任何一个请帮帮我,这是我的帖子数据代码

- (void)uploadPath:(NSString*)path withOptions:(NSDictionary*)options withImageData1:(NSData*)data1 ofImageName1:(NSString *)imageName1 andImageData2:(NSData*)data2 ofImageName2:(NSString *)imageName2 {  
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 
    NSURL *url = [NSURL URLWithString:path relativeToURL:[NSURL URLWithString:baseURLString]];
    NSMutableURLRequest *mutableRequest = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:90.0];

    NSString *boundary = @"-------1234567";

    [mutableRequest setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary] forHTTPHeaderField:@"Content-Type"];
    [mutableRequest setHTTPMethod:@"POST"];
    NSMutableData *postbody = [NSMutableData data];

    for(NSString *key in options)
    {
        NSString *value = [options objectForKey:key];
        [postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", key] dataUsingEncoding:NSUTF8StringEncoding]];
        [postbody appendData:[value dataUsingEncoding:NSUTF8StringEncoding]];
    }   
    [postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    //*******************   Append 1st Image    ************************/

    if ([imageName1 isEqualToString:@"picture1.jpg"]) {
        [postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"picture1\"; filename=\"%@\"\r\n",imageName1] dataUsingEncoding:NSUTF8StringEncoding]];
    }
    [postbody appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postbody appendData:[NSData dataWithData:data1]];
    [postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    //*******************   Append 1st Image    ************************/

    //*******************   Append 2nd Image    ************************/   

    if ([imageName2 isEqualToString:@"picture2.jpg"]) {
        [postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"picture2\"; filename=\"%@\"\r\n",imageName2] dataUsingEncoding:NSUTF8StringEncoding]];
    }
    [postbody appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postbody appendData:[NSData dataWithData:data2]];

    //*******************   Append 2nd Image    ************************/

        [mutableRequest setHTTPBody:postbody];
    urlConnection = [[NSURLConnection alloc] initWithRequest:mutableRequest delegate:self];
    if (self.urlConnection) {
        self.dataXml = [NSMutableData data];
    }
    self.urlConnection = nil;
    [mutableRequest release];
    [pool release];
}

2 个答案:

答案 0 :(得分:3)

找出错误的一种解决方案:

  1. 使用ASIHttpRequest
  2. 制作新项目
  3. 获取Charles
  4. 等网络​​调试代理
  5. 打开Charles(或其他),执行成功的多文件上传。 将数据从HTTP流量记录到服务器。
  6. 现在运行您自己的代码。记录该流量。有什么不同 你的工作流量是多少?使你的所有人都具有身份 尊重,怎么可能不起作用?
  7. 您还可以使用curl命令行替换步骤(1),该命令行将多个文件上传到服务器并记录该流量,但我在curl中不够精通我的头脑是什么样的命令。

答案 1 :(得分:1)

- (NSString*) nameValString: (NSDictionary*) dict {//example CHLOVA_FORM_BOUNDARY is @"aaa"
NSArray* keys = [dict allKeys];
NSString* result = [NSString string];
int i;
for (i = 0; i < [keys count]; i++) {
    result = [result stringByAppendingString:
              [@"--" stringByAppendingString:
               [CHLOVA_FORM_BOUNDARY stringByAppendingString:
                [@"\r\nContent-Disposition: form-data; name=\"" stringByAppendingString:
                 [[keys objectAtIndex: i] stringByAppendingString:
                  [@"\"\r\n\r\n" stringByAppendingString:
                   [[dict valueForKey: [keys objectAtIndex: i]] stringByAppendingString: @"\r\n"]]]]]]];
}

return result;
}

- (void)sendPhoto{
NSString *param = [self nameValString:dic];//dic is post info like {lat=36;lng=120;}
NSString *footer = [NSString stringWithFormat:@"\r\n--%@--\r\n", CHLOVA_FORM_BOUNDARY];

param = [param stringByAppendingString:[NSString stringWithFormat:@"--%@\r\n", CHLOVA_FORM_BOUNDARY]];
param = [param stringByAppendingString:@"Content-Disposition: form-data; name=\"pic\";filename=\"image.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n"];

NSData *jpeg = UIImageJPEGRepresentation([UIImage imageWithContentsOfFile:_path], 0.55);
//NSLog(@"jpeg size: %d", [jpeg length]);

NSMutableData *data = [NSMutableData data];
//img one
[data appendData:[param dataUsingEncoding:NSUTF8StringEncoding]];
[data appendData:jpeg];
[data appendData:[footer dataUsingEncoding:NSUTF8StringEncoding]];
//img two
[data appendData:[param2 dataUsingEncoding:NSUTF8StringEncoding]];
[data appendData:jpeg2];
[data appendData:[footer2 dataUsingEncoding:NSUTF8StringEncoding]];

NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", CHLOVA_FORM_BOUNDARY];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPShouldHandleCookies:NO];
[request setHTTPMethod:@"POST"];
[request setValue:contentType forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [data length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:data];
//then do sending
}