无法上传图像并将其保存在服务器上

时间:2011-05-17 08:24:46

标签: ios objective-c iphone

以下是我上传图片的代码。 图片已上传,但服务器上传图片的大小为 0 KB

    NSData *imageData = [NSData dataWithContentsOfFile:filePath];   

    NSString *str_url = [NSString stringWithFormat:@"http://someUrl.aspx?TaskId=%@",ID];

    NSURL *url=[NSURL URLWithString:str_url];

    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:url];
    [request setHTTPMethod:@"POST"];
    NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@,",boundary];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];

    NSMutableData *body = [NSMutableData data];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];    
    [body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"UploadedFilePhysical\"; filename=\"%@\"\r\n",Phusicalstr1]] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:imageData]];
    //[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

知道上传中发生了什么吗?

1 个答案:

答案 0 :(得分:-1)

 NSMutableData *body = [NSMutableData data];

这条线是做什么用的?

你是如何初始化的?

用这样的东西替换它。

NSMutableData *body = [[NSMutableData alloc] init];