Link to home
Start Free TrialLog in
Avatar of Sh_Rashed
Sh_Rashed

asked on

downloading file to user machine

Hi experts,

I have mp3 files in the server and I have jsp page that display all these files. I need when the user click on the "save", the file is downloaded to his machine.

Thanks
Avatar of Kuldeepchaturvedi
Kuldeepchaturvedi
Flag of United States of America image

you can just create a link to the file and browser will download it...

<a href="yourfile">Filename</a>
ASKER CERTIFIED SOLUTION
Avatar of Mayank S
Mayank S
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Sh_Rashed
Sh_Rashed

ASKER

I have tried the code in the url

 System.out.println("My file is:111111111111111111"+request.getParameter("file"));
   
    String  filename = request.getParameter("file");
    String original_file = "/CourseFiles"+filename;
   
        File                f        = new File(filename);
        int                 length   = 0;
        ServletOutputStream op       = response.getOutputStream();
        ServletContext      context  = getServletConfig().getServletContext();
        String              mimetype = context.getMimeType( filename );
   
       response.setContentType( (mimetype != null) ? mimetype : "application/octet-stream" );
       response.setContentLength( (int)f.length() );
       response.setHeader( "Content-Disposition", "attachement; filename=\"" + original_file+ "\"" );
        byte[] bbuf = new byte[1000];
        DataInputStream in = new DataInputStream(new FileInputStream(f));

        while ((in != null) && ((length = in.read(bbuf)) != -1))
        {
            op.write(bbuf,0,length);
        }

        in.close();
        op.flush();
        op.close();
       

but I got this Exception  1.mp3 (The system cannot find the file specified)

the file I want to download it is mps file audio
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank u so much
it works fine in firefox, but not in IE7?

help plz? :)
what error you are getting??
the quicktime plugin in the IE7 shows question mark image
instead of opening up directly to quicktime, save to the disc and then run it with quicktime to see if the file is coming down correctly
Then the quicktime plugin is not properly configured with IE.
but when I open the url of mp3 in IE, quicktime plays the file.

anyway, Thanks for helping me :)
>> response.setHeader( "Content-Disposition", "attachement; filename=\"" + original_file+ "\"" );

attach'E'ment with an extra E in between or attachment? I guess that is the issue. One of the versions of IE had this bug, not sure which one.