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

标签 .net ios xcode upload image-upload

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

最佳答案

所以我让它工作了。我所更改的只是来自

的边界字符串
NSString *boundary = @"- - - - - - - - - - Boundary String - - - - - - - - - -";

对此

NSString *boundary = @"xxxxBoundaryStringxxxx";

我读过的所有内容都说它只需要是一个在其余发布数据中不存在的字符串。我不知道我的第一个字符串可能有什么,但可能还有其他我不知道的规定。无论哪种方式,它现在都有效。

关于.net - 将图像从 iPhone 上传到 .net ashx 处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10213634/

相关文章:

c# - 为什么找不到 System.ServiceModel.WebHttpBinding?

c# - Protobuf-net 与 C++ 的官方谷歌 Protobuf 不兼容(消息编码)

.net - 检查hangfire重复作业是否已经在运行

ios - 如何在两个 View Controller 之间共享mapkit的变量?

ios - "Default"MonoDevelop中的iPhone SDK版本

Xcode 期望堆栈 View 中的 UILabel 具有比实际高度更大的高度

asp.net - 在 VS2010 中,新的 asp "showat"属性要求不一致。为什么?

xcode - 如何在 Xcode 中引用使用不同配置构建的依赖项目?

c++ - 从 xcode c++ 程序终端调用 OSX 包

ios - 是否可以使用 AVAudioEngine 播放 iOS 音乐库中的歌曲?