0

How can I upload an image into my web service then I can save it on my server!

I have tried the below code using POST method but I got this error

(A potentially dangerous Request.Form value was detected from the client (uploadFile="<ffd8ffe0 00104a46 4...").)
func myImageUploadRequest() {
    let imageData = UIImageJPEGRepresentation(myImageView, 1)
    let boundary = generateBoundaryString()
    var base64String = imageData.base64EncodedStringWithOptions(.allZeros)

    let myUrl = NSURL(string: "http://xxxxx/UploadImageTest");
        let request = NSMutableURLRequest(URL:myUrl!);
        request.HTTPMethod = "POST";
    request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")


    if(imageData==nil) { return; }
    var body = NSMutableData();
    body.appendString("uploadFile=\(imageData)")
    request.HTTPBody = body
    let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
        data, response, error in

        if error != nil {
            println("error=\(error)")
            return
        }

        // You can print out response object
        println("******* response = \(response)")

        // Print out reponse body
        let responseString = NSString(data: data, encoding: NSUTF8StringEncoding)
        println("****** response data = \(responseString!)")

        dispatch_async(dispatch_get_main_queue(),{
        });

    }

    task.resume()
}

And this the POST SOAP on my Asp.net web service

    POST /xxxx.asmx/UploadImageTest HTTP/1.1
    Host: xxxx.com
Content-Type: application/x-www-form-urlencoded Content-Length: length

uploadFile=string&uploadFile=string

2 Answers 2

3

The error was sending NSData to Asp.net web service.

  1. convert the image int base64 using this code

    let imageData = UIImageJPEGRepresentation(myImageView, 1)
    var base64String = imageData.base64EncodedStringWithOptions(.allZeros)
    
  2. then send it to web service and make sure in your web service receive String data, not byte() array.

  3. in your web service convert the base64 into Image and save into your server.

That's it!.

Sign up to request clarification or add additional context in comments.

Comments

1

I am not familiar with Swift but in Objective C I would have prepared data something like this:

....
NSMutableString* body = [NSMutableString new];
[body appendFormat:@"uploadFile=%@",base64String];
....

The reason it is erring out is pretty obvious from the error description. You might find your code is formatting your base64 image string as

uploadFile="<ffd8ffe0 00104a46 4...

The text after uploadFile= is not at all a base64 encoded string rather the string representation of NSData and that extra < at the starting of the data is being treated as a html tag in server. ASP.NET request validation does not allow such tags in your request body as security measure, to prevent code/script injection and cross site scripting.

Even if you disable request validation from server web.config or .asmx the data would still not be interpreted by server as it would still not be in valid base64 format as the server might expect.

So my advice is to frame your request properly before sending it to server and everything should work seamlessly.

1 Comment

Thanks for your comment was helpful for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.