当我将文件发送到服务器时,如何将文件分成几个部分?

时间:2011-09-15 15:51:31

标签: objective-c

我正在尝试在Objective C中编写代码,该代码应该将JPEG文件发送到服务器。问题是该文件被分成几个部分,只有第一部分到达那里。有办法解决这个问题吗?

以下是一些代码:

int j;
for (j = 0; j < 5; j++) {        
    // Read in data from appropriate signature file
    NSMutableString *imagePath = [folder_path_2 mutableCopy];
    [imagePath appendString:fn[j]];
    [imagePath appendString:@".jpeg"];
    NSLog(imagePath);

    NSData *imageData = nil;
    BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:imagePath];
    if (fileExists) {
        imageData = [[NSData alloc] initWithContentsOfFile:imagePath];
    } else {
        NSLog(@"JPEG image file does not exist.");
    }

    request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:[NSURL URLWithString:urlStr]];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"image/jpeg" forHTTPHeaderField:@"Accept"];
    [request setValue:@"image/jpeg" forHTTPHeaderField:@"Content-Type"];

    int len = (int)[imageData length];
    length_str  = [NSString stringWithFormat: @"%d", len];
    [request setValue:length_str forHTTPHeaderField:@"Content-Length"];
    postBody = [NSMutableData data];
    [postBody appendData:[NSData dataWithData:imageData]];

    [request setHTTPBody:postBody];

    // Make connection to the Internet
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil
                                                       error:nil];
    NSString *returnString = (NSString*)[[NSString alloc] initWithData:returnData
                                                          encoding:NSUTF8StringEncoding];
    NSLog(returnString);
}

int j; for (j = 0; j < 5; j++) { // Read in data from appropriate signature file NSMutableString *imagePath = [folder_path_2 mutableCopy]; [imagePath appendString:fn[j]]; [imagePath appendString:@".jpeg"]; NSLog(imagePath); NSData *imageData = nil; BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:imagePath]; if (fileExists) { imageData = [[NSData alloc] initWithContentsOfFile:imagePath]; } else { NSLog(@"JPEG image file does not exist."); } request = [[[NSMutableURLRequest alloc] init] autorelease]; [request setURL:[NSURL URLWithString:urlStr]]; [request setHTTPMethod:@"POST"]; [request setValue:@"image/jpeg" forHTTPHeaderField:@"Accept"]; [request setValue:@"image/jpeg" forHTTPHeaderField:@"Content-Type"]; int len = (int)[imageData length]; length_str = [NSString stringWithFormat: @"%d", len]; [request setValue:length_str forHTTPHeaderField:@"Content-Length"]; postBody = [NSMutableData data]; [postBody appendData:[NSData dataWithData:imageData]]; [request setHTTPBody:postBody]; // Make connection to the Internet NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; NSString *returnString = (NSString*)[[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; NSLog(returnString); }

1 个答案:

答案 0 :(得分:0)

您尝试发送的jpeg文件有多大?