PHP to save the file to the local issue of JS

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.