Recently engaged in a LiveZilla online customer service chat, the customer interface requirements to add a download chat record function. So I just looked over the internet on the various "JS save files to the local" information, found only in IE under the execcommand implementation. So again a toss ah. Let's get down to the dinner.
HTML tags
<TD<!--fu_hidden-->><aID= "Download"Target= "_blank" ><imgclass= "Lz_chat_clickable_image"onclick= "Top.savefile ();"src= "./images/button_file.gif"Border= "0"title= "<!--lang_client_save_file-->"alt= "<!--lang_client_save_file-->"></a></TD>
JS function
function SaveFile () {var result= '; var iframe = window.frames["lz_chat_frame.3.2"];var chatframe =iframe.document.all[ "lz_chat_frame.3.2.chat.4.0"].contentwindow.document;var topframe=iframe.document.all["Lz_chat_ frame.3.2.chat.0.0 "].contentwindow.document;//text is the chat information obtained from the page var text = Chatframe.getelementbyid (' Lz_chat_main ' ). innertext;//Add the Connection property and its value Topframe.getelementbyid (' Download ') to the label. SetAttribute ("href", "./chatnote.php?data=" +text) ;}
Mode one: Through a label, get the data to the server, and then the server to create a new file, save the transmitted content, and then read the contents of the file, the most recent download operation. This approach is mainly for me to familiarize myself with PHP's new files and read files. If you need to download it directly, please check the second method.
PHP code
<?PHP$dirname=Date(' Ymd ', Time()); $filename=Date(' Ymdhis ', Time()); //Create a folder named the current date under the Chatrecord directory $filepath= "chatrecord/".$dirname; Mk_dir ($filepath); //looping through the creation of catalogsfunctionMk_dir ($dir,$mode= 0755) { if(Is_dir($dir) || @mkdir($dir,$mode))return true; if(!mk_dir (dirname($dir),$mode))return false; return@mkdir($dir,$mode); } //Get contentHeader(' content-type:text/html; Charset=utf-8 ');$backValue=$_get[' Data ']; $filename= ' chatrecord/'.$dirname.‘ /‘.$filename.‘. txt;//writing content to a file$myfile=fopen($filename, "W") or die("Unable to open file!");fwrite($myfile,$backValue);fclose($myfile);$filesize=filesize($filename);$now=Date(' his ', Time());$downfilename= ' Customer service record '.$now.‘. TXT ';//Download the file nameHeader("Content-type:application/force-download");Header("Content-disposition:attachment; Filename= ".$downfilename);Header("Content-length:".)$filesize);$data=file_get_contents($filename);//Get file ContentsEcho $data;?>
Mode two: Do not save files on the server, directly download files
<?PHP//Get contentHeader(' content-type:text/html; Charset=utf-8 ');$backValue=$_get[' Data ']; $now=Date(' his ', Time());$downfilename= ' Customer service record '.$now.‘. TXT ';//Download the file nameHeader("Content-type:application/force-download");Header("Content-disposition:attachment; Filename= ".$downfilename);$data=$backValue;//Get file ContentsEcho $data;?>
PHP to save the file to the local issue of JS