Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download file with original file name

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?

like image 716
Amogh Avatar asked Apr 02 '13 10:04

Amogh


People also ask

How do I find the original file name?

1 Correct answer In the Metadata panel there is a field below File Name called Original Filename.It¨s visible when Metadata is set to Exif and IPTC. In the Metadata panel there is a field below File Name called Original Filename.

How do I change a file name before downloading it?

Click on the "Edit Menu" > Preferences > General tab. Locate the "Save downloaded files to" section, Click on "Downloads" > "Other"... Browse and indicate your new download location. Was this article helpful?

What is wget content disposition?

Use wget --content-disposition <url> Explanation: The Content-Disposition header can be used by a server to suggest a filename for a downloaded file. By default, wget uses the last part of the URL as the filename, but you can override this with --content-disposition , which uses the server's suggested name.

What is a download type file?

A DOWNLOAD file is a placeholder file that represents an in-process or paused Internet download. DOWNLOAD files are created by the Apple Safari web browser, which is bundled with the macOS and iOS operating systems. ​You can double-click a DOWNLOAD file to resume a Safari download. DOWNLOAD file open in macOS Finder.


2 Answers

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:)

like image 67
Hunter Zhao Avatar answered Oct 04 '22 00:10

Hunter Zhao


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 + "\"");
}
like image 45
Bozho Avatar answered Oct 04 '22 02:10

Bozho