0

Currently, I'm using this code:

<?php
header('Content-type: application/force-download');
header('Content-Disposition: attachment; filename="'.$_POST['savename'].'.bob"');
echo($_POST['savedata'])
?>

It gets the data of a form, creates an attachment and sends it right back. This is useless, because the client has the data. I would like to rewrite it to:

myForm.onsubmit = SomeSaveToFileFunction()
3
  • 1
    Setting aside modern HTML5 APIs (which almost certainly don't do what you really want anyway), you can't tell a browser to save a file to the local (client) machine. Commented Apr 5, 2011 at 13:24
  • 1
    I think you should add an explanation of what you are trying to achieve here. Simply posting code and saying "do this in javacript" isn't very helpful. Commented Apr 5, 2011 at 13:25
  • I'm trying to get some text in a text file... Is the title unclear? Commented Apr 6, 2011 at 15:34

3 Answers 3

2

Chrome can do that now.

html

<textarea></textarea><button>save</button>

Javascript

document.getElementsByTagName('button')[0].onclick = function(){

  var a = document.createElement('a');

  a.href = 'data:text/plain;base64,'+
    btoa(document.getElementsByTagName('textarea')[0].value);

  a.textContent = 'download';
  a.download = 'text.txt';
  a.click();

}

DEMO

http://jsfiddle.net/88BYB/3/

EDIT the latest version of chrome does not accept the download attribute.(chrome35)

Sign up to request clarification or add additional context in comments.

Comments

1

The traffic might be useless, indeed. You're right. But like Pointy said, unfortunatelly there's no way by simply using javascript.

If you would just like to have your download implemented in a more elegant way (than page reload), make use of a "hidden" iframe, that loads the PHP-response. Maybe you could easily switch the PHP processing from POST to GET and ...

myForm.onsubmit = function(formValueSavename, formValueSavedata) { 
// phpIframeRef addresses you hidden iframe
phpIframeRef.loaction.href = 'sendData.php?savename=' + formValueSavename + '&savedata=' + formValueSavedata;
}

You'll propably need some code to suppress your default handling of the form... which I don't know.

1 Comment

Thanks, but there's far too much data for GET. If you want to use the code of Heiko, you can suppress the default handling with "return false;" at the end of the function. Page reload isn't necessary if you submit the form to a blank page with "<form target='_blank'>".
0

JavaScript cannot directly write files on a local computer, for obvious security reasons. However, you might be able to do just what you need using ActiveX object, but once again, it will only work with IE. You would then need equivalents like NPAPI to make it compatible with Chrome for example.

I guess you should stay with the way you actually do it, if it works like you want it to.

EDIT: I meant NPAPI and not "API". It lets you build plug-ins that work (kind of) the same way ActiveX do, and lets you do the same things.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.