1

I have a pretty standard upload form - user picks a file, then hits the upload button. But now I need to add data to the post - after the user picks the file, but obviously before the post leaves the browser. Note that the post is a direct post to a third party (Amazon S3).

Is there a way to make a form with a file picker (or something that looks like one), then the user picks a local file, then hits a button, => javascript calls my server, gets a response, builds the 'real' post and then sends this new post to a third party server.

Basically, until the user picks the file, I don't know a few things (mime type). I know that browsers send this info, but Amazon AWS pre signed posts ignore what the browser says.

I can hit my server (ruby sinatra) with an ajax call from the javascript, which will return some JSON, etc to the script, which will then post to Amazon S3.

My problem could just be my newbieness to javascript...

pguardiario
  • 53,827
  • 19
  • 119
  • 159
Tom Andersen
  • 7,132
  • 3
  • 38
  • 55
  • What do you mean "add data to the post"? Why not just have more form fields in the same form? – Diodeus - James MacFarlane Dec 01 '11 at 21:57
  • Is it enough to be able to read the file extension and use that for "mime type"? – James Montagne Dec 01 '11 at 21:57
  • The request is an 'AWS pre-signed post' - I need to generate the entire post after I have the mime type, as changing a field (other than the file that's to be uploaded), invalidates the post. – Tom Andersen Dec 01 '11 at 22:08
  • I will likely get the mime type by looking at the extension, unless I can get it from some header field in the post? I have not got to that yet. – Tom Andersen Dec 01 '11 at 22:09
  • Here is the Amazon bug that I am trying to work around. https://forums.aws.amazon.com/thread.jspa?threadID=18616 – Tom Andersen Dec 01 '11 at 22:51
  • Another way is that after the upload, I could rewrite the file (S3 -> S3 copy so no data on the wire), at which point you can set the content-type. – Tom Andersen Dec 01 '11 at 22:53

1 Answers1

2

You can add hidden fields to the form with the file in it. Do your intermediate request, fill the hidden fields. Submit the original form.

Halcyon
  • 57,230
  • 10
  • 89
  • 128
  • Like this: http://stackoverflow.com/questions/133925/javascript-post-request-like-a-form-submit I guess, with an ajax tossed in? – Tom Andersen Dec 01 '11 at 22:13
  • https://forums.aws.amazon.com/thread.jspa?threadID=81348&tstart=0 for a more detailed description of what I did. Thanks to the comments and answers.. – Tom Andersen Dec 02 '11 at 18:11