ios - AFHTTPRequestOperationManager 文件上传返回 500 服务器错误

标签 ios afnetworking

我正在尝试使用 afnetworking 将相机拍摄的照片上传到网络服务。

这是我的上传部分代码:

CGSize newSize = CGSizeMake(500.0f, 500.0f);
UIGraphicsBeginImageContext(newSize);
[_imagedata drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *imgData = UIImageJPEGRepresentation(newImage, 0.9f);

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSDictionary *parameters = @{@"user": "test_user"};
    [manager POST:@"http://www.mywebsite.com/webservice.aspx" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        [formData appendPartWithFormData:imgData name:@"image"];
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Success: %@", responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];

我使用 Asp.net 作为我的网络服务。

这是我的服务器端代码:
    string USER = Server.UrlDecode(Request["user"]),
    SqlParameter prmUser = sc.Parameters.Add("@user", SqlDbType.VarChar);
    prmUser.Direction = ParameterDirection.Input;
    prmUser.Value = USER;


    HttpFileCollection MyFileCollection = Request.Files;
    if (MyFileCollection != null && MyFileCollection.Count > 0 && MyFileCollection[0] != null)
    {
        SqlParameter prmImage = sc.Parameters.Add("@Image", SqlDbType.Image);
        prmImage.Direction = ParameterDirection.Input;
        byte[] buf = new byte[MyFileCollection[0].ContentLength];
        MyFileCollection[0].InputStream.Read(buf, 0, MyFileCollection[0].ContentLength);
        prmPhoto.Value = buf;

    }
sc.ExecuteNonQuery();

现在每次我运行程序时都会出现这个错误:
Error: Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: internal server error (500)" UserInfo=0x1555ff60 {com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x16a94010> { URL: http://www.mywebsite.com/webservice.aspx } { status code: 500, headers {
    Connection = close;
    "Content-Length" = 3420;
    "Content-Type" = "text/html; charset=utf-8";
    Date = "Wed, 01 Apr 2015 15:56:21 GMT";
    Server = "Microsoft-IIS/8.5";
    Via = "1.1 magellan-front (squid/3.5.1)";
    "X-AspNet-Version" = "4.0.30319";
    "X-Cache" = "MISS from magellan-front";
    "X-Powered-By" = "ASP.NET";
} }, NSErrorFailingURLKey=http://www.mywebsite.com/webservice.aspx, NSLocalizedDescription=Request failed: internal server error (500),

详细错误:
[HttpRequestValidationException (0x80004005): A potentially dangerous Request.Form value was detected from the client (image=&quot;...�5�Ƨ&amp;�����&lt;I
�(�ep��K�,�=mp�...&quot;).]

该网络服务也适用于 android Http文件上传方法。

最佳答案

我试图找到这个的基础,这里是 link

从您的代码看来,客户端似乎有问题。你为什么不尝试使用 appendPartWithFileURL:name:fileName:mimeType:error: ,并说如果这不起作用,那么问题必须来自服务器端。

我对 ASP.NET 了解不多,但您能否验证一下您尝试读取文件的方式。这里是 link我在哪里发现了一些你可能想尝试实现的有趣的东西。
HttpFileCollection 上的枚举器返回文件的键(名称),而不是 HttpPostedFileBase对象。获得 key 后,使用 Item ( [] ) 属性和键 (文件名) 以获取 HttpPostedFileBase目的。

foreach (string fileName in Request.Files)
{
    HttpPostedFileBase file = Request.Files[fileName];

    ...
} 

如果这不起作用,请告诉我。

关于ios - AFHTTPRequestOperationManager 文件上传返回 500 服务器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29395678/

相关文章:

ios - 在 iOS 中以编程方式适配方面?

ios - iOS 中的库(.a 文件)或框架,哪个是更好的选择

objective-c - 使用 AFNetworking 2.0 签署 API 请求

使用 AFNetworking 没有证书的 iOS SSL Pinning?

ios - 如何将AFHTTPClient,Afnetworking 1.0迁移到2.0

iphone - 如何动画 View 之间的转换?

ios - 子类如何继承父类委托(delegate)?

ios - 使用 AFNetworking 不需要缓存 GET 请求

iphone - AFNetworking —— GET 使用什么?

android - 如何在 ionic 应用程序中查找按钮是否处于 Activity 状态