0

My apologies if this is an inappropriate question:

My Problem

I'm creating (or attempting to create) a RESTful API for an application that utilizes file uploads primarily for use with a JavaScript client. As many of you know, there's no standard, cross-browser way to upload files using Ajax (outside of the iframe method). Unfortunately, in my application uploading the file requires authentication and so there's no way to use the iframe method that couldn't potentially disrupt the UI.

I obviously could POST to "https://username:password@myhost/resource" but the credentials could then be saved in the browser. Making logging out very difficult.

My Solution

So MY solution is base64 encoding. Which goes as follows:

  1. Use an iframe to post file to my script (let's call it base64.php)
  2. That script converts the file to base 64 and then returns the string.
  3. The application takes the string and sends that in place of the file to my API
  4. The API takes the base 64 string and converts it back to the file.
  5. File "upload" complete

Essentially, I use the iframe method to post a file to a script that takes the script, encodes it, and returns the string. I then perform my ajax request as normal using the Base64 encoded string rather then the file itself.

My Question

My question is does anyone see any problems with this solution? Is there a better way? Or is there anything I can do to improve it?

Thanks!

Community
  • 1
  • 1
KTastrophy
  • 1,739
  • 15
  • 23

1 Answers1

0

The big problem that I see is that your file gets uploaded twice: First to base64.php, then by the AJAX - effectively doubling your bandwidth usage, and therefore making the user wait twice to upload one file. There's an HTML 5 solution here, if you're interested: http://net.tutsplus.com/tutorials/javascript-ajax/uploading-files-with-ajax/

Grinn
  • 5,370
  • 38
  • 51