我正在使用接收图像的 ashx 脚本将图像从 iPhone 上传到我的服务器。我遇到的问题是,无论我尝试过什么, context.Request.Files 都是空的。
这是我的xcode代码
NSData *imageData = [[appDelegate currentProjectData] objectForKey:@"frontImage"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.****.com/iPhoneJpgUploadHandler.ashx"]];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"- - - - - - - - - - Boundary String - - - - - - - - - -";
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:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\"iphoneimage.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"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]];
[request setHTTPBody:body];
[request addValue:[NSString stringWithFormat:@"%d", [imageData length]] forHTTPHeaderField:@"Content-Length"];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"%@", returnString);
这是我上传脚本中的代码
public void ProcessRequest (HttpContext context) {
string uploadDir = "C:\\";
if(context.Request.Files.Count > 0) {
HttpPostedFile file = context.Request.Files[0];
file.SaveAs(Path.Combine(uploadDir, file.FileName));
context.Response.Write("success");
} else {
context.Response.Write("fail");
}
}
xcode 中的 nslog 写出“失败”,因此它正在运行脚本并返回。我没有收到任何错误。知道我做错了什么吗?