Mp4 Download causes browser to play file instead of download

后端 未结 5 1714
情书的邮戳
情书的邮戳 2020-12-16 17:44

In my website I stream users mp4 content. I also allow users to download. However in Chrome it seems to automatically play the file in an internal player instead of download

5条回答
  •  甜味超标
    2020-12-16 18:14

    if you want a cross browser solution

    you need a server-side code to download the file

    example: I am working on jsp technology, if you can use jsp in your website you can try the following code in the file download.jsp:

    <%@ page import="java.io.*, java.lang.*, java.util.*" %>
    <%
    String filename=request.getParameter("filename");   
    response.setContentType("application/octet-stream");
    response.setHeader("Content-Disposition",
    "attachment;filename="+filename);
     %>
    
    <%
    /*
     File file = new File(filepath+filename );*/
     String path = getServletContext().getRealPath("/mp4/"+filename); 
     File file = new File(path);
    FileInputStream fileIn = new FileInputStream(file);
    ServletOutputStream out1 = response.getOutputStream();
    
    byte[] outputByte = new byte[4096];
    //copy binary contect to output stream
    while(fileIn.read(outputByte, 0, 4096) != -1)
    {
        out1.write(outputByte, 0, 4096);
    }
    fileIn.close();
    out1.flush();
    out1.close();
     %>
    

    you can put the code above in a file: download.jsp

    then in your page links you will use it like:

    song1
    

    with my best wishes to you

提交回复
热议问题