Upload from local file via AJAX?

So, I’m idly musing about an AJAX-based S3 file manager and it occurs to me: I don’t know how to do file uploads via AJAX PUT requests. Is there any way to get access to a local file selected in an input element in a form? That’d sure make a nice alternative to pasting data into a big textarea.

10 Comments

  1. Posted April 6, 2006 at 10:05 am | Permalink

    I’ve yet to find a way to automate an http file upload within the browser. You can’t write to the builtin file upload input element’s filename. Possibly a Flash helper would be of use - see http://www.aflax.org for instance.

    btw - I’ve sent some emails to your pobox addr lately with no ack. Either I’m a bit stinky or you’re not getting them or a bit of both.

  2. Avatar Martin Atkins
    Posted April 6, 2006 at 12:29 pm | Permalink

    I don’t think that you can do that right now, but read-only access to the file pointed to by a file upload element sounds like a good thing to propose to the WHATWG people for inclusion in their Web Applications spec, if it isn’t already there.

    As long as it’s read-only and you can’t actually put a filename into that box from script I don’t really see any security flaw to that.

  3. Posted April 7, 2006 at 6:01 pm | Permalink

    gmail is AJAXing up attachments already. I don’t really know how they’re pulling it off.

  4. Posted April 8, 2006 at 4:38 am | Permalink

    i’m not sure gmail is ajaxing attachments, i think they’re using a clever combination of iframes and dom manipulation of the input boxes, with perhaps a javascript timer auto-triggered onsubmit().

    just guessing there, but i think you can see it if you have a “view rendered source” plugin for your browser…

  5. Posted April 10, 2006 at 7:45 am | Permalink

    No way to do it in a browser using the current technologies - without changing security restrictions, etc. I did implement an “AJAX style” uploader using ActiveX to browse for a file, base64 encode it and then post it to the server using the XHR object - this was before “AJAX”, but not brain surgery, of course you did have to let IE trust the site to allow the ActiveX stuff access to your hard drive - could’ve signed it I guess, but too much trouble for an Intranet app. GMail is just use an auto submit when you choose a file; in IE they are using a little bit of JS to auto popup the dialog box so you don’t have to have a browse button visable. The AFLAX stuff looks interesting, though.

  6. Avatar Fred
    Posted May 26, 2006 at 10:23 am | Permalink

    Gmail does not use ajax to upload files. This link shows how we can upload files the way gmail does:
    http://blog.360.yahoo.com/blog-qCsFWG45eqF9lZ05AZldsUC.?p=58

  7. Avatar ellinas
    Posted July 18, 2006 at 1:36 pm | Permalink

    I just hit the wall with the same problem.
    My current idea is the following:
    - Open a popunder (not to make it annoyingly visible)
    - set this window as receiver to the form
    - trigger a submit by javascript parallel to the Ajax request
    - after upload the popunder can call a jS function in the opener page that talks to the server and receives the uploaded pic for example
    - then the popunder closes itself

    Could this be a solution?

  8. Posted July 30, 2006 at 7:26 am | Permalink

    ellinas:

    Most AJAX file upload solutions work almost exactly as you describe, however they use an invisible iFrame instead of a brand new window.

    For example, the upload_progress plug-in for Ruby on Rails uses this method to provide upload progress in real time.

  9. Posted September 1, 2006 at 11:21 am | Permalink

    The invisible IFrame works great. I have a script that has suited me fine. You can see it at http://www.seemysites.net/projFolder/uploader - it’s completely free, too.

  10. Posted September 1, 2006 at 12:24 pm | Permalink

    Nope, the iframe approach won’t work for Amazon S3 - which is what this post is really looking for. I’m looking for a 100% client-side solution that uses Amazon’s S3 api. So, no form post uploads, no server-side PHP helpers, just browser-to-S3 HTTP PUT. I don’t think it’s presently possible.

Post a Comment

Your email is never shared. Required fields are marked *

*
*