1

Im trying to upload camera taken photo to webservice with afnetworking.

this my upload part code :

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);
    }];

Im using Asp.net as my webservice.

this is my server side code :

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

now everytime i run program this error 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),

the detailed error :

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

also the webservice works well with android HttpFileUpload method.

Ahad Porkar
  • 1,666
  • 2
  • 33
  • 68
  • Please set the responseSerializer before posting. You are setting it inside the Post. – Jassi Apr 04 '15 at 12:35
  • This could be useful http://stackoverflow.com/questions/81991/a-potentially-dangerous-request-form-value-was-detected-from-the-client – Pablo A. Apr 07 '15 at 13:34
  • The following links could be useful http://stackoverflow.com/a/29198665/730807,http://stackoverflow.com/a/29198483/730807 – Durai Amuthan.H Apr 08 '15 at 16:45

2 Answers2

2

I was trying to find the basics of this, here is the link

What it seems from your code is that the client side seems to have a issue. Why don't you try using appendPartWithFileURL:name:fileName:mimeType:error:, and say if that doesn't work then the issue has to be from server side.

I don't know much about ASP.NET, but could you once verify your way how you are trying to read the file. Here is the link where in I found something interesting you might wanna try implementing.

The enumerator on the HttpFileCollection returns the keys (names) of the files, not the HttpPostedFileBase objects. Once you get the key, use the Item ([]) property with the key (filename) to get the HttpPostedFileBase object.

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

    ...
} 

Let me know if this doesn't work.

Community
  • 1
  • 1
Amit Singh
  • 1,075
  • 1
  • 7
  • 18
1

Try uploading the image as a file (appendPartWithFileData) rather than as form data (appendPartWithFormData), and setting a content-type:

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

Also see https://stackoverflow.com/a/15413152/1469259

Community
  • 1
  • 1
CupawnTae
  • 14,192
  • 3
  • 29
  • 60