AFHTTPRequestOperationManager 文件上传 returns 500 服务器错误

AFHTTPRequestOperationManager file upload returns 500 server error

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

Web 服务与 android HttpFileUpload 方法配合使用也很好。

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

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

另见

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

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

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

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

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

    ...
} 

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