Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set name of file downloaded from browser?

I'm writing a web application that, among other things, allows users to upload files to my server. In order to prevent name clashes and to organize the files, I rename them once they are put on my server. By keeping track of the original file name I can communicate with the file's owner without them ever knowing I changed the file name on the back end. That is, until they go do download the file. In that case they're prompted to download a file with a unfamiliar name.

My question is, is there any way to specify the name of a file to be downloaded using just HTML? So a user uploads a file named 'abc.txt' and I rename it to 'xyz.txt', but when they download it I want the browser to save the file as 'abc.txt' by default. If this isn't possible with just HTML, is there any way to do it?

like image 655
Sparafusile Avatar asked Jun 23 '10 13:06

Sparafusile


People also ask

How do I change a downloaded file name?

There are many ways to rename a file in Windows. The easiest way is by right-clicking on the file and selecting Rename. You can then type a new name for your file and press enter to finish renaming it. A quicker way to rename a file is by first selecting it by left clicking on it, then pressing the F2 key.

How do I change the download name in Google Chrome?

Note: If your address bar is at the bottom, swipe up on the address bar. Tap Downloads. Step 5: From the given option tap Rename. Step 6: Enter your new file name.

Why does a file name change when downloaded?

Each time a file is added to the Downloads folder, this action will run, and it will automatically rename files with names containing "%20" to use spaces instead.


1 Answers

When they click a button to download the file, you can add the HTML5 attribute download where you can set the default filename.

That's what I did, when I created a xlsx file and the browser want to save it as zip file.

<a href="path/to/file" download="renamed.txt">Download</a> <a href="downloads/export.xlsx" download="Data-Export.xlsx">Download Export</a> 
like image 103
Mephiztopheles Avatar answered Sep 21 '22 12:09

Mephiztopheles