1
我正在使用接收图像的ashx脚本将图像从iPhone上传到我的服务器。我遇到的问题是,无论我尝试过的任何内容context.Request.Files都是空的。将图像从iPhone上传到.net ashx处理程序
这里是我的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写出了“失败”,所以它是运行脚本并返回。我没有收到任何错误。任何想法我做错了什么?