|
How to download files using a chrome extension?
I want to download text file generated in a textarea
here my download.php file code structure goes :
<?php
if(empty($_POST['filename']) || empty($_POST['content'])){
exit;
}
$filename = preg_replace('/[^a-z0-9\-\_\.]/i','',$_POST['filename']);
header("Cache-Control: ");
header("Content-type: text/plain");
header('Content-Disposition: attachment; filename="'.$filename.'"');
echo $_POST['content'];
?>
and the javascript used
(function($){
$.generateFile = function(options){
options = options || {};
if(!options.script || !options.filename || !options.content){
throw new Error("Please enter all the required config options!");
}
var iframe = $('<iframe>',{
width:1,
height:1,
frameborder:0,
css:{
display:'none'
}
}).appendTo('body');
and the script.js
$(document).ready(function(){
$('#download').click(function(e){
$.generateFile({
filename : 'export.txt',
content : $('textarea').val(),
script : 'download.php'
});
e.preventDefault();
});
by these assets i created a web page consisting a TEXTAREA , by entering some text on the textarea and clicking on the download button , a text files is generated and downloaded on the user's system, I packed the source code as a CRX file to use it as a CHROME extension, but while using the extension,the files are'nt generated, while the webpage works absolutely fine !!!
|