2015-04-01 70 views
1

我试图通过网络上传相机拍摄的照片到web服务。AFHTTPRequestOperationManager文件上传返回500服务器错误

这是我上传的部分代码:

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作为我的web服务。

这是我的服务器端代码:

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(); 

现在每次我运行程序这个错误apears:

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方法。

+0

请在发布之前设置responseSerializer。你正在把它设置在邮政内。 – Jassi 2015-04-04 12:35:01

+0

这可能是有用的http://stackoverflow.com/questions/81991/a-potentially-dangerous-request-form-value-was-detected-from-the-client – 2015-04-07 13:34:00

+0

下面的链接可能是有用的http:// stackoverflow的.com /一个/730807分之29198665,HTTP://stackoverflow.com/a/29198483/730807 – 2015-04-08 16:45:18

回答

2

我试图找到这里的基本知识,这里是link

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

我对ASP.NET不太了解,但是你能否验证一下你如何试图读取文件?这里是link,在那里我发现了一些有趣的东西,你可能想尝试实现。

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

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

    ... 
} 

让我知道这是行不通的。

1

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

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

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

相关问题