0

I'm generating a report in my application and saving it somewhere in my PC. Now when user clicks on download button a Save as window should open and the file should get written to the specified location with the specified name. I'm able to do writing of file to another file but I'm unable to get the Save as window.

How to download a file from save as window using Java and Javascript?

Sorry! The types of file can be either HTML or PDF or CSV.The content of file is table containing few values and results.

Its a desktop application. Right now I'm saving the file to my hard coded location.After saving if ever user clicks download button,a Save as window should open so that the user can save the five to his specified location.I'm using Java,Spring,Hibernate and JavaScript.

2
  • 1) What type of file is it? As in, what is the file extension? What is in it? 2) "Please anybody help... Thank you." Leave such noise out of questions. Commented Apr 17, 2013 at 11:17
  • It is very unclear what you are asking here. Are you talking about a web application or a desktop application? Commented Apr 17, 2013 at 11:23

2 Answers 2

1

Use JFilechooser

String wd = System.getProperty("user.dir");
JFileChooser fc = new JFileChooser(wd);
int rc = fc.showDialog(null, "Select Data File");
if (rc == JFileChooser.APPROVE_OPTION)
{
File file = fc.getSelectedFile();
filename = file.getAbsolutePath();
// call your function here
}
else
System.out.println("File chooser cancel button clicked");
return;
Sign up to request clarification or add additional context in comments.

Comments

0

Please make sure that your download url sends the correct content type.

here is a littel helper method I have created:

protected void SetContentType(ContentType type)
        {
            switch (type)
            {
                case ContentType.HTML:
                    {
                        _context.Response.ContentType = "text/html";
                        break;
                    }
                case ContentType.JSON:
                    {
                        _context.Response.ContentType = "application/json";
                        break;
                    }
                case ContentType.Text:
                    {
                        _context.Response.ContentType = "text/plain";
                        break;
                    }
                case ContentType.PDF:
                    {
                        _context.Response.ContentType = "application/pdf";
                        break;
                    }
                case ContentType.OctetStream:
                    {
                        _context.Response.ContentType = "application/octet-stream";
                        break;
                    }
                case ContentType.Excel:
                    {
                        _context.Response.ContentType = "application/vnd.ms-excel";
                        break;
                    }
                default:
                    {
                        _context.Response.ContentType = "application/json";
                        break;
                    }
            }
        }

if you want the file to always download use the application/octet-stream option

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.