Nick
Nick

Reputation: 9373

iOS upload image to server via ASP

I am trying to build a backend in pure ASP to handle image uploads and am having some difficulty. I found this file upload class which works through a form. The code works great but stripping out the form elements and just passing the file path via POST seems like a bad approach. I've been trying to find upload files via FTP in asp but didn't find much info.

Any ideas or alternatives for uploading pictures from iOS to the server via ASP securely and safely?

Upvotes: 0

Views: 319

Answers (1)

Brian Nickel
Brian Nickel

Reputation: 27550

If you're looking to only pass the image as part of the request body, this is pretty easy to do.

In iOS, you would set the request.HTTPBody = <your data here> and in ASP, you could read the body using Request.BinaryRead(Request.TotalBytes). For performance reasons you would probably want to break up the binary read into smaller parts of the file, but that's basic file parsing.

If you want to pass additional parameters, you will need a bit more work since neither NSURLRequest nor classic ASP have a native understanding of multipart requests. You're on the right track with the file upload class, as it will handle multipart on the server side. For the client, I recommend using a framework like AFNetworking. From their FAQ, you can do something like this:

NSData *imageData = UIImagePNGRepresentation(image);
NSURLRequest *request = [client multipartFormRequestWithMethod:@"POST"
        path:@"/upload" parameters:nil 
        constructingBodyWithBlock:^(id <AFMultipartFormData> formData) {

    [formData appendPartWithFileData:imageData name:@"avatar"
            fileName:@"avatar.png" mimeType:@"image/png"];
}];

Upvotes: 1

Related Questions