将图像从iPhone上传到.net ashx处理程序

我正在使用接收图像的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写出“失败”,所以它正在运行脚本并返回。 我没有得到任何错误。 任何想法我做错了什么?

所以我得到它的工作。 所有我改变的是来自边界的string

 NSString *boundary = @"- - - - - - - - - - Boundary String - - - - - - - - - -"; 

对此

 NSString *boundary = @"xxxxBoundaryStringxxxx"; 

我读过的所有内容都只是一个string,并不包含在其他的数据中。 我没有看到我的第一个string可以如何,但可能有其他规定,我不知道。 无论哪种方式,现在它的作品。