动态更改下载的文件名 [英] Dynamically change the downloaded filename

查看:102
本文介绍了动态更改下载的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用php动态更改下载的文件名?这个excel表是使用Javascript从HTML表转换的。现在我想在每次下载时动态地更改文件名。我正在使用输入类型按钮作为下载按钮。







编辑注意:
根据评论,此问题可能会被重新写成如下。


我希望人们能够从服务器下载Excel Sheet。如何强制文件下载并根据月份设置文件的名称?当我下载文件时,显示download.xls



没有代码提供



查看评论:




解决方案

你写


这个excel表是使用Javasc从HTML表转换的


这意味着HTML表在客户端已经可以作为EXCEL文件使用。



如果是这样,没有理由将转换的EXCEL文件从客户端推回服务器,所以客户端可以再次从服务器下载它。在无用的循环中这样做并不浪费带宽。



换句话说:当EXCEL文件已经驻留在客户端的Javascript内存中时,您需要做的就是将(JavaScript转换的)EXCEL文件存储到文件名就像Javascript的 Math.random()。对于一个简单的文件保存方法,HTML5的文件保存功能(作为许多方法之一)可能成为您最好的朋友。



想想:如果你坐在家里的沙发上,你不开车去开车回家,拿起啤酒你家的厨房里的冰箱,你呢?对,你只是呆在家里,走进厨房抓住那个啤酒。 ;)



相同:如果您将客户端转换为客户端,客户端将该文件保存在计算机的内存中,请将其保存到他的磁盘上。不要强迫他上传整个文件,所以他可以下载它...如下所示:Javascript的 Math.random()使您能够生成一个随机数,您可以使用创建随机文件名。



编辑



根据评论中的OP问题


,但是如何按照月份将这些下载的文件保存为动态文件名。


要根据这个月创建一个文件名,你可以像这样使用Javascript的日期:

  var now = new Date(); 
var month = now.getMonth();

如果您正在寻找有关如何存储文件客户端的提示,则可能需要潜入:





如果要处理服务器端,并使用PHP根据年月日使用您自己的文件名向客户端推送文件,您可以这样:

 <?php 
$ realname ='./data.xls'
if(file_exists($ realname))
{
$ randname ='YourExcelFilename'.gmdate('Y-m-d')'。xls'
header('Content-type:application / vnd.ms-excel');
header('Content-Disposition:attachment; filename ='。$ randname);
header('Content-Length:'.filesize($ realname));
header('Expires:0');
header('Cache-Control:must-revalidate');
header('Pragma:public');
ob_clean();
flush();
readfile($ file);
}
exit();
?>

编辑2 ):通过删除2不必要的标题感谢@Herbert的评论



如果您想要更多的替代解决方案,您可能需要检查我在第一条评论中链接的两个问题,因为您的问题是这些问题的潜在重复


How do I dynamically change the downloaded filename using php? This excel sheet was converted from an HTML table using Javascript. Now I want to dynamically change the filename each time it is downloaded. I'm using an input type button as the download button.


Editorial Note: Based on comments, this question may be reworded as follows.

I want people to be able to download an Excel Sheet from the server. How do I force a file download and set the name of the file based on the month? When I download the file it shows “download.xls”

No code provided

See comments:

解决方案

You write

This excel sheet was converted from an HTML table using Javascript.

That would mean the HTML table is available as EXCEL file at the client's side already.

If that is the case, there is no reason to push the converted EXCEL file from the client back to the server, just so the client can download it from the server again. That wouldn't make sense and waste bandwidth on a useless loop.

In other words: when the EXCEL file already resides in the client's Javascript memory, all you need to do is to store your (Javascript converted) EXCEL file to a filename based on something like Javascript's Math.random(). As for an easy approach to file saving, things like HTML5's file-saving capabilities (as one of many ways to do it) may become your best friends.

Think about it: if you're sitting on your sofa at home, you don't drive to work just to be able to drive back home and fetch a beer from the fridge in your home's kitchen, do you? Right, you just stay at home and walk into the kitchen to grab that beer. ;)

Same here: if you convert it client-side and the client has the file in his computer's memory, let him save that onto his disks. Don't force him to upload the whole file just so he can download it… as said: Javascript's Math.random() enables you to produce a random number which you can use to create random filenames.

EDIT

Based on OP's question in comment:

but how to save these downloaded files with a dynamic file name according to the month.

To create a filename based on the month, you would use Javascript's Date like this:

var now = new Date(); 
var month = now.getMonth();

And if you're looking for hints on how to store your file client-side, you might want to dive into:

If you want to handle it server-side and push the file towards the client using PHP with your own filename based upon the year-month-day, you could go like this:

<?php
$realname = './data.xls';
if (file_exists($realname )) 
{
    $randname = 'YourExcelFilename'.gmdate('Y-m-d').'.xls';
    header('Content-type: application/vnd.ms-excel'); 
    header('Content-Disposition: attachment; filename='.$randname);
    header('Content-Length: '.filesize($realname));
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    ob_clean();
    flush();
    readfile($file);
}
exit();
?>

(EDIT 2: Corrected the above example by removing 2 unnecessary headers thanks to a comment by @Herbert)

If you want more alternative solutions, you might want to check the answers to the two questions I linked in my first comment since your question is a potential duplicate of those questions

这篇关于动态更改下载的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆