Download file with original file name

http response download file
content-disposition
content-disposition: inline; filename
java code to download a file from server
spring download file from url
android download file from url
download file from https url in java
spring mvc download file from database

In my project I am uploading a file. While uploading, I am saving its original file name and extension in a database and saving that file with some GUID on server, generated GUID is also stored in database along with file name and extension.

For example-

-File name for uploading is questions.docx

-Then orignalFileName will be "questions"

-FileExtension will be ".docx"

-File be get uploaded with file name as "0c1b96d3-af54-40d1-814d-b863b7528b1c"

Uploading is working fine..but when I am downloading some file it gets downloaded with file name as the GUID in above case its "0c1b96d3-af54-40d1-814d-b863b7528b1c".How can I download a file with its original file name i.e "questions.docx".

Code Added

    /**
     * code to display files on browser
     */
    File file = null;
    FileInputStream fis = null;
    ByteArrayOutputStream bos = null;

    try {
        /**
         * C://DocumentLibrary// path of evidence library
         */
        String fileName = URLEncoder.encode(fileRepo.getRname(), "UTF-8");
        fileName = URLDecoder.decode(fileName, "ISO8859_1");
        response.setContentType("application/x-msdownload");            
        response.setHeader("Content-disposition", "attachment; filename="+ fileName);
        String newfilepath = "C://DocumentLibrary//" + systemFileName;
        file = new File(newfilepath);
        fis = new FileInputStream(file);
        bos = new ByteArrayOutputStream();
        int readNum;
        byte[] buf = new byte[1024];
        try {

            for (; (readNum = fis.read(buf)) != -1;) {
                bos.write(buf, 0, readNum);
            }
        } catch (IOException ex) {

        }
        ServletOutputStream out = response.getOutputStream();
        bos.writeTo(out);
    } catch (Exception e) {
        // TODO: handle exception
    } finally {
        if (file != null) {
            file = null;
        }
        if (fis != null) {
            fis.close();
        }
        if (bos.size() <= 0) {
            bos.flush();
            bos.close();
        }
    }

Is this code is perfect?

You should set your origin file name into the response header, like below:

String fileName = URLEncoder.encode(tchCeResource.getRname(), "UTF-8");
fileName = URLDecoder.decode(fileName, "ISO8859_1");
response.setContentType("application/x-msdownload");            
response.setHeader("Content-disposition", "attachment; filename="+ fileName);

Hope to help you:)

File download with original filename · Issue #162 · shrinerb/shrine , Hi Thanks for this awesome gem. - Really appreciate. How do I download the file with its original filename? currently, the file gets downloaded  In my project I am uploading a file. While uploading, I am saving its original file name and extension in a database and saving that file with some GUID on server, generated GUID is also stored in database along with file name and extension. For example- -File name for uploading is questions.docx-Then orignalFileName will be "questions

You just fetch the originalName from the database and set it in the Content-Disposition header:

@RequestMapping("/../download")
public ... download(..., HttpServletResponse response) {
  ...
  response.setHeader("Content-Disposition", "attachment; filename=\"" + original + "\"");
}

wget: how to download a served file keeping its name, XX:5001/read in a browser (with browser cache disabled) the file is returned with its original name. Using wget, alas, I cannot preserve the file  Using vba and excel, I'm looking to download a file from an url for which I'll not know what the original filename is. Using URLDownloadToFile, or other methods as with WinHttpRequest, expects you to name the file you are downloading. I would like to save with the original filename, as one would do by navigating to the url on internet explorer.

You can set the file name in the header.

Say for Example you are using RestFul Webservice then ResponseBuilder can be used like this:

ResponseBuilder rsp = Response.ok("Your Content Here", "application/docx");    
rsp.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");

The content type and file name both are/can be set at run time.

readfile - Manual, header('Content-Disposition: attachment; filename='.basename($file)); but the function DownloadFile($file) { // $file = include path Download the source. Is it possible to let your user download a file with a different name? For example, there is a file called "4324ffsd34.jpg". I want people to download it via download.php, with a different name (like "filetodownload.jpg"), without renaming the original file.

Hi @Amogh Use below code

response.setHeader("Content-Disposition", "attachment; filename="+FILENAME+".docx");

Downloads, Give curl a specific file name to save the download in with -o [filename] (with --​output as the long version of the option), where filename is either just a file name,​  Download File Name Fix - Trim file names for multiple files and folders by choosing custom strings to remove, and automatically process spaces, tabs, brackets, and underscores

To name a file with current Date in response header you can use following code:

final String Date_FORMAT = "dd/MM/yyyy"; 
Date currentDate = new Date();
SimpleDateFormat formatDate = new SimpleDateFormat(Date_FORMAT);
String datenew = formatDate.format(currentDate);

response.setContentType("application/pdf");//for pdf file
response.setHeader("Content-disposition","attachment;filename="+ datenew +"Smoelenboek.pdf");

Date_Format can be any date format you want!! :)

enter code here

Manage Files and Folders in Course Files, When you overwrite files in Course Files, file names and link titles in your course aren't affected. Download files and  The value of the attribute will be the name of the downloaded file. There are no restrictions on allowed values, and the browser will automatically detect the correct file extension and add it to the file (.img, .pdf, .txt, .html, etc.). If the value is omitted, the original filename is used.

Is there any way to make Mozilla Firefox automatically rename , When using Google, if I download two files named "unknown" it will save the second Firefox will add (number) to the file name when the file is  A file extension is the characters after the last dot in a file name. For example, in the file name "winmail.dat", the file extension is "dat". It helps Windows select the right program to open the file.

Filename, A filename or file name is a name used to uniquely identify a computer file stored in a file For example, a compiler might use the extension FOR for source input file (for Fortran code), OBJ for the object Download as PDF · Printable version. For an example, a file name “payment.xls” will get changed to “payment.xls.[original_filename].!Shadow” when this malware attack targeted system. After encrypting all the important data on victim’s PC, it demand a huge sum of ransom money to unlock files through ransom note left behind on the infected machine.

Unique (but not Insane) Filenames > All about Uploading Files in , I told the "UploadedFile" object to move the file into "public/uploads". a private file storage, even storing viruses and trying to trick people into downloading it from our This will give us the original filename - astronaut.jpg - but without the file  We would like to show you a description here but the site won’t allow us.

Comments
  • where should i pass file path of server?
  • Will this work if I'm using the HttpServletResponse OutputStream to send bytes?
  • @Hunter Zhao I have followed the same above, But am receiving the file name as "53228afe-553c-4ddb-8097-9bd77ba89809.docx". I dont ve any idea what am missing
  • What is the type of tchCeResource ?
  • here "original" means just orignal file name or full path file on server.. like in my example C://DocumentLib/0c1b96d3-af54-40d1-814d-b863b7528b1c
  • Is it possible to make this without HttpServletResponse class?
  • This work if your filename contains only ASCII characters. Otherwise you have to encode it.
  • stackoverflow.com/questions/53445477/… please help
  • what is ResponseBuilder? And Content-Disposition should have the words capitalized
  • "application/docx" can i pass this part dynamically coz i may have .mp3,.xml,.etc file.
  • @JSS what is ResponseBuilder??
  • @Amogh: Yes, ContentType can be set dynamically at run time. The ResponseBuilder example is for RestFul webservices. As you didn't specify what technology you were using when you asked the question so I just used this for example. If you are using a normal servlet (as can be seen in your example now) then same can be achived by using HttpServletResponse object. Other folks have given the examples.
  • @Bozho: I have now given the link for ResponseBuilder in my post. The above example is for RestFul webservices.