2015-04-01 71 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

相關問題