0

On the client side I want to:

  1. Generate random data up to a specified size (ex: 1Mb or 512Kb).

  2. Write that data into a file.

  3. Post that file to an endpoint on my server with an AJAX request.

I know #1 is possible, but I'm not sure how to do it. I'm not sure if #2 is possible; I've heard that Chrome currently supports the full HTML5 file API, but I need a solution that works on mobile browsers too (iOS Safari, Android, etc). #3 is easy and I already know how to do it (I included it for clarity about what I'm trying to do).

emersonthis
  • 32,822
  • 59
  • 210
  • 375
  • Define "Write that data into a file" — do you want to store that file on the file system of the client or just transfer the data to the server as a file-like message? – Quentin Aug 07 '13 at 00:15
  • How's #3 easy? Are you able to successfully post an arbitrary file from the filesystem with JavaScript? – bfavaretto Aug 07 '13 at 00:15
  • 3
    Is there a reason that you need to write to a file? You can pass in any arbitrary data (eg: a string variable) via a POST with AJAX without the need for a file. – Marplesoft Aug 07 '13 at 00:25
  • @Quentin The latter. I don't want to save the file to the system. Just send it with a post request. – emersonthis Aug 07 '13 at 00:53
  • @bfavaretto I just meant I know how to send a basic AJAX request. – emersonthis Aug 07 '13 at 02:02
  • @Marplesoft The reason I want to generate a file is because I'm using it to test the user's upload speed. It doesn't seem to be accurate to test with just data. At least, I tried it with the following approach and it was way off every time: http://stackoverflow.com/questions/15472251/estimate-users-upload-speed-without-direct-permission – emersonthis Aug 07 '13 at 02:08
  • imo, switch 2 and 3 and have the server make the file and the problem becomes a cake walk. If you use HTML5 the problem isn't that hard either. Without HTML5, file uploads are usually done with iframes, flash or activex controls. The only way the file is going to get the server with an AJAX request is if you convert it to a string first and write it to a file on the server (defeating the point of making the file on the client in the first place). tldr: file upload with ajax is not possible (at least not on all browsers: http://stackoverflow.com/questions/2320069/jquery-ajax-file-upload). – zclark Aug 07 '13 at 03:34
  • @zclark I'm not having any trouble submitting files with ajax. I'm using the jquery form plugin and it seems to work smoothly in IE8+ which is good enough for my purposes. As for the rest of what you said, I'm a little confused. The point is to test upload speed from client to my server. If the file is already on my server it doesn't help me do that. Obviously I could have the user download a file and then re-upload it, but that's exactly what I'm trying to avoid (and the point of this question). Perhaps I've misunderstood what you're proposing. If so, consider submitting it as an answer. – emersonthis Aug 07 '13 at 17:52

1 Answers1

-1

I agree with with sending data with post, but if you really want to create a file and then send it, you should read this question:

How to give a Blob uploaded as FormData a file name?

Community
  • 1
  • 1
Entity Black
  • 3,401
  • 2
  • 23
  • 38
  • 1
    My take-away from that question is that they couldn't figure out how to do it cross-browser. Did I overlook something? – emersonthis Aug 08 '13 at 12:53
  • Well it depends what do you consider as a cross browser. For example in our company we don't support IE6+7 anymore. Also some projects are developed for a long time so we are basicly making apps for IE9-10. Take a look: https://developer.mozilla.org/en-US/docs/Web/API/Blob#Browser_compatibility Imho everything that supports blob should be able to send blob in post. Since IE10 is able to work with blob, it is cross browser technique for me. It is your choice. – Entity Black Aug 08 '13 at 13:08
  • The thread you referenced discusses setting the name of a blob. This is only possible in Chrome and the latest version of Firefox. – Ray Nicholus Aug 08 '13 at 13:14
  • Yes, becouse the question itself in that thread is solution of this question. Well it is not compatible with all current browsers, but it could be soon + nobody here mentioned blob files before so... what is your point? – Entity Black Aug 08 '13 at 13:36