【问题标题】:AFHTTPRequestOperationManager file upload returns 500 server errorAFHTTPRequestOperationManager 文件上传返回 500 服务器错误
【发布时间】:2015-04-01 16:09:56
【问题描述】:

我正在尝试使用 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;).]

webservice 也适用于 android HttpFileUpload 方法。

【问题讨论】:

标签: ios afnetworking


【解决方案1】:

我试图找到这方面的基础知识,这里是link

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

我对 ASP.NET 了解不多,但您能否验证一下您尝试读取文件的方式。这是link,我在其中发现了一些您可能想尝试实施的有趣内容。

HttpFileCollection 上的枚举器返回文件的键(名称),而不是 HttpPostedFileBase 对象。获得密钥后,使用 Item ([]) 属性和密钥(文件名)来获取 HttpPostedFileBase 对象。

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

    ...
} 

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

【讨论】:

    【解决方案2】:

    尝试将图像上传为文件 (appendPartWithFileData) 而不是表单数据 (appendPartWithFormData),并设置内容类型:

    [formData appendPartWithFileData:imgData name:@"image"
        fileName:@"image.jpg" mimeType:@"image/jpeg"];
    

    另见https://stackoverflow.com/a/15413152/1469259

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多