0

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.

My question is, is there any way to specify the name of a file to be downloaded.? So a user uploads a file named 'abc.pdf' and I rename it to '10.pdf', but when they download it I want the browser to save the file as 'abc.pdf' by default. is there any way to do it?

I referred this question

How to set name of file downloaded from browser?

But in my case, I am opening pdf by in new tab from javascript. when clicking on pdf will get the id which is equal to the name stored in the server, with that id will refer DB and fetch actual name using ajax. But How will I rename the filename from id to actual name?

    $(document).on('click', '.file', function (e) { 
    var id = this.id+'.pdf';
    $.ajax({
                url: "<?php echo base_url() ?>/Directorylist/files",
                        type: "POST",
                        datatype: 'json',
                        success: function (data) {  
    var actual_name = data; 
    var win = window.open();                    
win.location.href="http://localhost:8080/panExplorer/uploads/"+id+"";
                        },
                });
    });
stevin
  • 53
  • 7
  • The answer will be the same, you need to redirect to server script and put header in that script. It doesn't matter how the redirect is triggered. – jcubic Sep 10 '19 at 11:16
  • Can you please explain it, I am just beginner. – stevin Sep 10 '19 at 11:18
  • You can use `"http://localhost:8080/panExplorer/download.php?file=id"` and in download.php write your header and read the pdf file from uploads directory. And also I've added answer with just JavaScript. – jcubic Sep 10 '19 at 11:24

1 Answers1

1

You can try to create a tag append it to body (it work without this in chrome but it require to be in document in Firefox) and click on it, (you need to call native click not jQuery method).

function download(url, fname) {
    var a = $('<a/>').attr({
      href: url,
      download: fname,
      target: '_blank'
    }).appendTo('body');
    a[0].click(); // native DOM function
    a.remove();
}

and instead of

var win = window.open();
win.location.href="http://localhost:8080/panExplorer/uploads/"+id+"";

use:

download("http://localhost:8080/panExplorer/uploads/" + id, "some_other.pdf");

EDIT:

The other solution would be to create php file that will be proxy in downloading the files and you use same code but forward to the php file:

var win = window.open();
win.location.href="http://localhost:8080/panExplorer/download.php?file="+id;

and in download.php file you use:

// and you should check if there are no ".." in the filename,
// so no one will download your source code with `file=../index.php`
readfile("./upload/" . $_GET['file']); 

and use solution from to download files How to set name of file downloaded from browser?

EDIT:

if you need to open the file but no download here is solution:

You need url that will have proper name so it need to end with /filename.pdf, the simplest way is to use url that look something like this:

  /UploadFiles/id/your_filename.pdf

and you need to write script (or route in in your framework) that will open id and ignore the filename, the filename will be used by browser.

jcubic
  • 61,973
  • 54
  • 229
  • 402
  • @stevin I've assume you want to download the file, that way you need second solution of use htaccess to forward the file. this how you create site with multiple urls that actully run index.php. you can redirect so you URL in address bar have your name and open different name on the server. You can also redirect to download.php and inside you name the file it's some kind of pattern. – jcubic Sep 10 '19 at 11:47
  • @stevin if you don't want to trigger download and change name the only solution is to change the url using `htaccess` there are slightly different way you can do that. – jcubic Sep 10 '19 at 11:51
  • Actually I want to get the actual name on downloading but not auto-download. – stevin Sep 10 '19 at 12:06
  • @stevin you want id to be your name when download? – jcubic Sep 10 '19 at 12:12
  • @jubic not id, the actual name which I stored in DB. – stevin Sep 10 '19 at 12:14
  • Your both solution is worked fine, the download file name I can change, but I want to restrict forced download. Now Instead of viewing the pdf file, it is getting auto-download. – stevin Sep 10 '19 at 12:18
  • @stevin See my last solution. – jcubic Sep 10 '19 at 12:24
  • Using download.php file worked fine now, by mistakenly first i added header('Content-Disposition: attachment; filename="downloaded.pdf"'); then i changed to header('Content-Disposition: filename="downloaded.pdf"'); now its working fine without auto-download. – stevin Sep 10 '19 at 13:16