2

I was looking at what my own server accepts, and it is looking for JSON files, which i double checked with my colleagues and confirmed.

My question is this:

If I have a Javascript object, and need to sent it to the server as a file? How would that be done? I was thinking that I would need to use a hidden iframe or something and then submit it.

I was hoping there was some way to say:

var _JS_OBJ_ = {/* ... */}
var f = File(JSON.stringify(_JS_OBJ_));
// ...
// submit a multipart form -OR- $.ajax({.....});

I was hoping there was a way to create a virtual file which isnt actually saved to the client machine, which would alleviate the client-server file downloading restrictions. That way since it isnt saved, i could create a dynamic file to upload to a server via a AJAX call.

Fallenreaper
  • 10,222
  • 12
  • 66
  • 129
  • http://stackoverflow.com/questions/6255344/how-can-i-use-jquery-to-post-json-data – moonwave99 Aug 12 '13 at 16:43
  • I know how JSON works, and do that all the time, but the servers accept JSON FILES, not JSON objects.... So i was thinking that i would need to create a temp file, store a string in there, and then post that file to the server. – Fallenreaper Aug 12 '13 at 16:48
  • http://stackoverflow.com/questions/5587973/javascript-upload-file – jgillich Aug 12 '13 at 16:49
  • @Fallenreaper Incorrect. The server accepts a long string of 1's and 0's -- that at a slightly higher level is checked to be a string representing a JSON object. If it wants to save those on the disk drive as part of the file system, that is the server's business. – Paul Aug 12 '13 at 16:51
  • Yea, i also of input[type=file] but that is obtained through the local client machine. Can you tie a file input to that of a dynamically created javascript File? – Fallenreaper Aug 12 '13 at 16:51
  • @Paul would there be a different header used to say this is an incoming file though? I am unsure if that json uploaded via 'data' of a Ajax call would suffice or if there is something else i should do to encapsulate the JSON such that when the server recieves it, it says "OH This is a file." etc. If i submitted the json via DATA in saying it is a multipart file, or something would that work, or is there something i am missing. – Fallenreaper Aug 12 '13 at 16:55
  • @Fallenreaper there is no JSON object. JSON is a serialized representation of a javscript object - you send a string, the server takes care of saving to filesystem / db / whatsoever. – moonwave99 Aug 12 '13 at 16:55
  • Someone could set up a server to accept file uploads, and expect those files to contain json. That is pretty rare, it is just not done because it doesn't facilitate automation like ajax does. – Paul Aug 12 '13 at 16:57
  • @Paul that what i have to work with. So i guess i am the 1%. – Fallenreaper Aug 12 '13 at 16:57
  • @moonwave99 my mistake, wrong vocab. I have a javascript object that i want to save into a file and then upload that file to the server, without it saving locally (As there are sec issues when downloading files to a computer. – Fallenreaper Aug 12 '13 at 16:58
  • Try looking at http://stackoverflow.com/questions/166221/how-can-i-upload-files-asynchronously-with-jquery --- or maybe not. Not sure this will let you upload your own data as a file. – Paul Aug 12 '13 at 16:59
  • Perhaps an interesting question is "Can I use javascript to construct a virtual file upload of browser generated data from the browser to a server's standard receiving handler normally paired to a HTML file input tag?" – Paul Aug 12 '13 at 17:06
  • I mean, that sounds exactly like what i want to do.... Maybe i should reform my question such that it is properly answered. – Fallenreaper Aug 12 '13 at 17:09
  • It looks like FormData is a new HTML 5 object that helps with this. – Paul Aug 12 '13 at 17:14
  • Too bad, not all users of my page would be using HTML5. I feel forcing it would be bad news bears. *sigh* Maybe there is some sort of iframe hack i could use to obtain the same sort of outcome? – Fallenreaper Aug 12 '13 at 17:15
  • http://stackoverflow.com/questions/9622901/how-to-upload-a-file-using-jquery-ajax-and-formdata – Paul Aug 12 '13 at 17:15
  • The old way was to use flash or iframes. Some people were looking for progress indicators instead of pushing their own data, though. – Paul Aug 12 '13 at 17:17
  • http://caniuse.com/xhr2 – Paul Aug 12 '13 at 17:18
  • MMMM, what if i printed the Javascript to a hidden iframe, and then pushed the iframe to the server via a request... or something similar as the only stuff in the iframe would be the output of javascript... and in a way, that is a file that is loaded. sort of? – Fallenreaper Aug 12 '13 at 17:22
  • Sorry if all this wasn't helpful, might want to pin down your exact problem a bit more, since there are many interesting problems that may or may not be your problem. – Paul Aug 12 '13 at 17:25
  • I've never seen someone upload the contents of an iframe. The HTML file tag is the primary thing I would turn to, to set the browser up to send a file to the server. Apparently xhr2 and FormData let you set this up too, and FileReader lets you read local files, but all that stuff needs IE10 or a reasonable recent alternative. Nor familiar with flash. Anyway good luck. – Paul Aug 12 '13 at 17:30
  • @Paul you have beeen a help. Im sure something will turn up. Maybe an alternative would be to create a worker thread on the server that accepts the JS object, and creates the file on the server and pushes it to where it needs to go. – Fallenreaper Aug 12 '13 at 17:34

0 Answers0