在Firefox中显示“另存为”对话框

时间:2012-01-16 08:46:17

标签: javascript dojo

我正在使用dojo和javascript来创建一个应用程序。在我的应用程序中有一个文件菜单。单击文件>>另存为菜单项时,它应弹出一个另存为对话框。我正在使用firefox.its而不是弹出另存为对话框。

任何人都会帮助我

1 个答案:

答案 0 :(得分:1)

这需要在服务器端处理,因为您需要设置要下载的文件的mime-type和Content-Disposition标头,因为您没有说明您使用的是哪种服务器端语言,我提供了以下示例在PHP中,但您可以使用任何服务器端语言。这将强制客户端下载文件,如果用户的浏览器设置为不自动下载到特定位置,则会出现另存为弹出窗口。

 // make sure it's a file before doing anything!
 if(is_file($file_name))
 {

    /*
    Do any processing you'd like here:
        1.  Increment a counter
        2.  Do something with the DB
    3.  Check user permissions
        4.  Anything you want!
    */

     // required for IE
     if(ini_get('zlib.output_compression')) { ini_set('zlib.output_compression', 'Off');    }

     // get the file mime type using the file extension
     switch(strtolower(substr(strrchr($file_name,'.'),1)))
     {
         case 'pdf': $mime = 'application/pdf'; break;
         case 'zip': $mime = 'application/zip'; break;
         case 'jpeg':
         case 'jpg': $mime = 'image/jpg'; break;
         default: $mime = 'application/force-download';
     }
     header('Pragma: public');  // required
     header('Expires: 0');      // no cache
     header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
     header('Last-Modified: '.gmdate ('D, d M Y H:i:s', filemtime ($file_name)).' GMT');
     header('Cache-Control: private',false);
     header('Content-Type: '.$mime);
     header('Content-Disposition: attachment; filename="'.basename($file_name).'"');
     header('Content-Transfer-Encoding: binary');
     header('Content-Length: '.filesize($file_name));   // provide file size
     header('Connection: close');
     readfile($file_name);      // push it out
     exit();

 }
相关问题