0

I'd like to create a file, specifically a text file with a stringified JSON object, in the browser and then upload it to my Amazon S3 account. Is there an existing library I can use for this? If not, how do I actually do this? I've uploaded clients' files to S3 before, but never created one on the fly.

Cheers.

Che Kofif
  • 831
  • 1
  • 8
  • 22
  • Is this JavaScript in a browser or JavaScript in something server side (like node.js)? What problem are you having? Generating the JSON? Constructing a suitable HTTP request? If that latter, what format does the HTTP request need to be in? POST? PUT? multipart mime? Are you wanting to go directly from browser to S3? Can you trust every user who will have access to this with your S3 credentials? – Quentin Oct 30 '12 at 07:07
  • how would you upload it? ftp? sftp? git? HTTP GET? HTTP POST? – GottZ Oct 30 '12 at 07:15
  • My goal is zero user interaction - just create the file in memory and then upload it to S3. Any idea if this is even possible? This is built into a tool that allows the user to upload files to S3 manually anyway, the credentials is a non issue. My problem is on the technical side of - how to actually create a file in memory and perform an upload without the user having to save a file locally and choose it. – Che Kofif Oct 30 '12 at 07:16
  • How to upload: I currently upload images (that the user chooses using the File Chooser input field) using POST anyway, so using the same infrastructure would be preferable. – Che Kofif Oct 30 '12 at 07:17
  • I haven't figured out how to construct a multipart mime request from raw data yet, but these resources may help: http://stackoverflow.com/questions/9395911/sending-a-file-as-multipart-through-xmlhttprequest / http://dev.opera.com/articles/view/xhr2/ / http://www.html5rocks.com/en/tutorials/file/xhr2/ – Quentin Oct 30 '12 at 09:03

1 Answers1

-2

Create text file in JavaScript

The method using window.open is worth a try... You'd probably need to save the file first to do an upload.

I think you could also compose a formdata that contains file information on the fly and send that over...

Community
  • 1
  • 1
user1600124
  • 440
  • 2
  • 11
  • That would require me to create an actual file on the client's computer and then have him save it and upload it. My goal is zero user interaction - just create the file in memory and then upload it to S3. Any idea if this is even possible? – Che Kofif Oct 30 '12 at 07:14
  • Well.. I don't use s3, so I can only give some suggestions.. According to 'http://aws.amazon.com/articles/1434'. You can upload files with a form post. If you know the format of a form post.. you can create your own. That being said... since s3 accept form-submit to upload files.. I am sure it's doable to compose your file on the fly. – user1600124 Oct 30 '12 at 07:19
  • The upload part isn't the problem - as much as creating a file at Javascript level on the fly.... Frankly I'm not sure how to approach that. – Che Kofif Oct 30 '12 at 07:20
  • http://stackoverflow.com/questions/219827/multipart-forms-from-c-sharp-client There are many samples on composing a formdata in c#. Do some research on the format of a multipart/formdata, and try port those examples into javascript. That's what i'd try to do at least. – user1600124 Oct 30 '12 at 07:31