nginx 아래 전단 크로스 도 메 인 다운로드 파일

layui 를 사용 하여 파일 을 업로드 한 후 다운로드 해 야 합 니 다. 그러나 다운로드 파일 은 업로드 할 때 중복 되 지 않 기 위해 파일 이름 은 새로운 규칙 으로 저장 되 었 습 니 다. 파일 을 다운로드 할 때 원래 의 파일 이름 을 복원 해 야 합 니 다.그러나 크로스 도 메 인 문제 로 다운로드 할 수 있 지만 이름 을 바 꿀 수 없습니다. html 의 a 태그 속성 다운 로드 는 이름 을 바 꿀 수 없습니다. 몇 시간 이 걸 렸 습 니 다. 방법 을 찾 아 기록 하 십시오. html 의 form 폼 다운로드 파일 법: 배경
@RequestMapping(value = "url", method = RequestMethod.GET)
    public void downloadEdiFile(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String mainUrl = request.getParameter("fileUrl");//      ,      、   、token
        String fileUrl = mainUrl.substring(mainUrl.indexOf("/"),mainUrl.indexOf("?"));//    
        String mianFileName = mainUrl.substring(0,mainUrl.indexOf("/"));//   
        String fileExtension = fileUrl.substring(fileUrl.lastIndexOf("."));//    
        String fileName = mianFileName+fileExtension;//     (   )
        //        
        String filePath = "http://112.112.223.229:1013/fileload" + fileUrl;
        //        
        URL url = new URL(filePath);
        URLConnection conn = url.openConnection();
        InputStream txtIn = conn.getInputStream();
        //    
//        InputStream txtIn = new FileInputStream(new File(filePath));
        OutputStream os = response.getOutputStream();
        String userAgent = request.getHeader("user-agent").toLowerCase();
        if (userAgent.contains("msie") || userAgent.contains("like gecko")) {
            // win10 ie edge           ie
            fileName = URLEncoder.encode(fileName, "UTF-8");
        } else {
            //      
            fileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1");
        }
        response.setContentType("application/octet-stream; charset=UTF-8");
        response.setHeader("Content-Disposition", "attachment; filename=" + fileName);// @TODO        
        //     
        byte[] bytes = new byte[1024];
        //                  
        int len = 0;
        while ((len = txtIn.read(bytes)) > 0) {
            os.write(bytes, 0, len);
        }
        if (os!=null){
            os.close();
        }
        if (txtIn!=null){
            txtIn.close();
        }
    }

token 과 fileName 을 연결 하 는 간단 한 연결 경로 입 니 다.
html js 코드
//      
$(document).on('click', '.upload-btn', function () {
            var fileUrl = $(this).parent().parent().siblings("td[data-field='fileUrl']").children()[0].innerHTML;
            var fileName = $(this).parent().parent().siblings("td[data-field='fileName']").children()[0].innerHTML;
            if (fileName === null || fileName.length === 0) {
                fileName = radioData.procPlanName + "-" + ($(this).parent().parent().siblings("td[data-field='procRecipeName']").children()[0].innerText);
            }
            //fileUrl        ;fileName    
            downloadFile(fileUrl, fileName);
        });

        //form       ,           ,        ;     ,         iframe ,   target=\"mangguo\"    
        function iframeFileFlag() {
            if (iframeFlag === false) {
                iframeFlag = true;
                alert("         ");
            }
        }

        //    
        function downloadFile(fileUrl, fileName) {
            iframeFlag = false;
            var $downForm = $("
"); // $downForm.attr("action", zuulUrl + "/file/upload/url"); var $input = $(""); $input.attr("name", "fileUrl"); var mainUrl = fileName + fileUrl + "?_token=" + _token; $input.val(mainUrl); $downForm.append($input); $(document.body).append($downForm); // , $downForm.submit(); }

html

<button type="button" class="layui-btn upload-btn"><i class="layui-icon"></i>    </button>

<iframe name="mangguo" style="display: none" onload="iframeFileFlag()"></iframe>

ps: 코드 는 한 네티즌 의 답 을 참고 했다.그런데 그 화면 을 못 찾 겠 어 요. 감사합니다.

좋은 웹페이지 즐겨찾기