我正在尝试将块中的图像从iphone上传到服务器,我在服务器端有PHP代码,它读取数据并使用HTTP_RANGE附加到现有图像。
注意:使用浏览器中的html测试服务器端代码并运行正常。
我的服务器端代码只将POST作为httpMethod,所以我必须在帖子的正文中给出restMethod = PUT和HTTP_RANGE,如下所示: -
[bodyData appendData:[[NSString stringWithFormat:@"\r\n\r\n--%@\r\n", kHttpPostMimeBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[bodyData appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"restMethod\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[bodyData appendData:[[NSString stringWithString:@"PUT"] dataUsingEncoding:NSUTF8StringEncoding]];
[bodyData appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", kHttpPostMimeBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[bodyData appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"HTTP_RANGE\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[bodyData appendData:[[NSString stringWithFormat:@"%d-0",offset] dataUsingEncoding:NSUTF8StringEncoding]];
上传的图片为0KB。我也试过在一个块中只用restMethod = PUT和HTTP_RANGE = 0-0发送文件,它仍然是0KB,但如果我只做restMethod = PUT并省略HTTP_RANGE,则图像上传成功。
我真的很难过,请帮助!!
编辑::添加实际的HTTP请求: -
NSMutableURLRequest *req = [[NSMutableURLRequest alloc] initWithURL:url];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",
kHttpPostMimeBoundary];
[req addValue:contentType forHTTPHeaderField:@"Content-Type"];
[req addValue:msgLength forHTTPHeaderField:@"Content-Length"];
[req setHTTPMethod:@"POST"];
NSMutableData *bodyData = [NSMutableData data];
[bodyData appendData:[[NSString stringWithFormat:@"\r\n\r\n--%@\r\n", kHttpPostMimeBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[bodyData appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"restMethod\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[bodyData appendData:[[NSString stringWithString:@"PUT"] dataUsingEncoding:NSUTF8StringEncoding]];
if (offset > 0) {
[bodyData appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", kHttpPostMimeBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[bodyData appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"HTTP_RANGE\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[bodyData appendData:[[NSString stringWithFormat:@"%d-0",offset] dataUsingEncoding:NSUTF8StringEncoding]];
}
[bodyData appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", kHttpPostMimeBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[bodyData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"%@\"\r\n",fileName] dataUsingEncoding:NSUTF8StringEncoding]];
[bodyData appendData:[[NSString stringWithFormat:@"Content-Type: %@\r\n\r\n",mimeType]
dataUsingEncoding:NSUTF8StringEncoding]];
[bodyData appendData:fileData];
[bodyData appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",kHttpPostMimeBoundary]
dataUsingEncoding:NSUTF8StringEncoding]];
NSString *msgLength = [NSString stringWithFormat:@"%d", [bodyData length]];
[headerParamDict setValue:msgLength forKey:@"Content-Length"];
[req setHTTPBody: bodyData];