English 中文(简体)
是否有可能下载吉大港山区行动计划的档案?
原标题:Is it possible to download a file with HTTP POST?

Is it possible to download a file with HTTP POST? I know the "Get" way(windows.location), but in my case, there are a lot of param that should be passed to server

问题回答

You mean like this ?

    function IssuePostRequest(objData) 
    {
        var strPageURL = "about:blank";
        var strAction = "@Url.Action("GetPDF", "Home")/";
        //var strAction = "/popups/delete.aspx";

        var strWindowName = "MyEvilHttpPostInAnewWindow"; // ifrmDownload
        var iWindowWidth = 805;
        var iWindowHeight = 625;



        var form = document.createElement("form");
        form.setAttribute("id", "bla");
        form.setAttribute("method", "post");
        form.setAttribute("action", strAction);
        form.setAttribute("target", strWindowName);
        form.setAttribute("style", "display: none;");
        // setting form target to a window named  formresult 


        // Repeat for all data fields
        var hiddenField = document.createElement("input");
        hiddenField.setAttribute("name", "data");
        hiddenField.setAttribute("value", objData);
        form.appendChild(hiddenField);
        // End Repeat for all data fields


        document.body.appendChild(form);



        // creating the  formresult  window with custom features prior to submitting the form
        //window.open(test.html,  formresult ,  scrollbars=no,menubar=no,height=600,width=800,resizable=yes,toolbar=no,status=no );
        //JS_PopupCenterScreen(strPageURL, strWindowName, iWindowWidth, iWindowHeight);
        window.open(strPageURL, strWindowName);

        // document.forms[0].submit();
        //document.getElementById("xxx").click();
        form.submit();
    } // End Function IssuePostRequest

有了这一服务器代码:

    public FileResult GetPDF(string data)
    {
        //data = @"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGAAAAA8CAYAAACZ1L+0AAAABHNCSVQICAgIfAhkiAAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAAuhSURBVHic7Zx5kBT1Fcc/r2f2EhZQDq9IvBADiRoGROWaBXcWTCokhaIVb4scRaQUhlJMorCgUiizSoyliWKZMjGR9UghCswSaQVEgQZEJAoiQiJqonJ44B7TL3/0zO7M7Bw7uz0Dhv1WTc30r1+/95vf6/f7vd97r1tUlaMRaklfoB+wRnz69eHqhxytCgBQS7oBU4Du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sRPSxHQX6tK2sS6iJ4DLY9+qsikqvmv3Lt8+shd6a5xExGVuwy0lQJUdI62HsAEhIP+PYGQGQaq4pq7k/vm7K9e4Hc4j9/knEwA9kZEHvEoLyY266JwsCJjZuqSB5aWNDUeMwbVvtGmIhV7JnBdO/qRM1YER60P1LwcRjUQ17x1xbSKZ9vIogYnilCWjTANPgUeNcJB/5M4sQkT+CTLRQdxyjHWANXAUK/aI4BT42hUDc/cbNJfnDKuXmxN9jSuqgqZeX01QDyMCAkxIRHuzHb3xxAO+sM4Tsss4C2cpFCmvUA98AGwFif2dko46N/R+bqaw4zO19UcZriVkvy/hFoyCLglemgDM91+q1anAtJALemPEyfqjTO3X5WPV5p1KiAF1JJvAWGcwa8HJopPs+0N2oXONSAJakkvnGBjX5xqh9H5GnzoVEAC1JJyYClO8uQ54Dzx5fcJ/s4pKIroG1D+gvOg4S/FpwWpL+q0AEAt+QXOc1+vAmcUavDhKLeA6Ntza4D/AoPFp3sK3YejdieslgzAmeuXyWF8V8X/AGryz36xXfJpAAAAAElFTkSuQmCC";

        string base64Data = System.Text.RegularExpressions.Regex.Match(data, @"data:image/(?<type>.+?),(?<data>.+)").Groups["data"].Value;
        byte[] binData = Convert.FromBase64String(base64Data);

        byte[] ba = PdfHandler.ImageToPdf(binData);
        //System.IO.File.WriteAllBytes(@"d:	empmyba.pdf", ba);

        //return System.Convert.ToBase64String(ba);
        return File(ba, "application/pdf", "Chart.pdf");
    }

是的,《名册》要求的其余部分可以指示浏览器下载文件。 档案内容将与GET案件相同,作为吉大港警察局的答复发送。

象你一样,希望提出 Java字提出的《开放式守则》要求。 我认为,没有办法让浏览器把AJAX请求的结果作为下载处理。 即使将内容提要定在浏览器通常作为下载(例如“申请/使用”)提供的东西上,浏览器只能将数据存入XMLHttpRequest。

此外,正如你可能已经知道的那样,没有办法让窗户开门()发出长期合作倡议的请求。

我认为,最好的方式是提出美国宇宙航空研究开发机构的请求,在服务器上提出档案。 在浏览器上,当申请完成时,使用窗户开()下载生成的文档。

我利用这一方法解决了这一问题:

service.js

downloadExcel : function() {
    var mapForm = document.createElement("form");
    mapForm.target ="_self"||"_blank";
    mapForm.id="stmtForm";
    mapForm.method = "POST";
    mapForm.action = "your_Controller_URL";

    var mapInput = document.createElement("input");
    mapInput.type = "hidden";
    mapInput.name = "Data";
    mapForm.appendChild(mapInput);
    document.body.appendChild(mapForm);

    mapForm.submit();
}

<>strong>Spring Chief Code :

@Controller

@PostMapping(value = "/your_Controller_URL")
    public void doDownloadEmsTemplate( final HttpServletRequest request, final HttpServletResponse response)
            throws IOException, URISyntaxException {

        String filePath = "/location/zzzz.xls";
        logger.info("Excel Template File Location Path :" + filePath);
        final int BUFFER_SIZE = 4096;
        ServletContext context = request.getServletContext();
        String appPath = context.getRealPath("");
        String fullPath = appPath + filePath;
        File downloadFile = new File(fullPath);
        FileInputStream inputStream = new FileInputStream(downloadFile);
        String mimeType = context.getMimeType(fullPath);
        if (mimeType == null) {
            //mimeType = "application/octet-stream";
            mimeType = "application/vnd.ms-excel";
        }
        logger.info("MIME type: " + mimeType);
        response.setContentType(mimeType);
        response.setContentLength((int) downloadFile.length());
        String headerKey = "Content-Disposition";
        String headerValue = String.format("attachment; filename="%s"", downloadFile.getName());
        logger.info("File Download Successfully : ");
        response.setHeader(headerKey, headerValue);
        OutputStream outStream = response.getOutputStream();
        byte[] buffer = new byte[BUFFER_SIZE];
        int bytesRead = -1;
        while ((bytesRead = inputStream.read(buffer)) != -1) {
            outStream.write(buffer, 0, bytesRead);
        }
        inputStream.close();
        outStream.close();
    }

从某种意义上来说,每个吉大港定居和自由职业者协会都是“卸载档案”,但最好把它视为电文有效载而不是档案。 在大多数情况下,有效载荷是一份超文本文件,浏览器应当作为网页。 但是,它不是超文本文件吗? 浏览器应当向用户提供“Save as” dialog的zi子吗? 显然,浏览器必须确定反应的内容类型并正确处理。

浏览器确定内容类型的最常见方式之一是通过。 HTTP Header, 因此称“Content-Type”。 这个头盔的数值为奇米。 这是浏览器在填充特定物品时的关键,如在答复中填入了“pdf”档案时发射一根烟花。

注,并非所有浏览器1)以相同的方式确定内容类型,2)对内容类型的反应方式相同。 有时,你们不得不冒.,把你想要的行为从所有浏览者手中夺走。 所有服务器辅助技术使你能够设置吉大港山区的头盔。

除请求方法外,没有差别,也没有差别,以及你如何向服务器发送数据。 无论你是否使用GET还是POST,你处理答复的方式都是一样的。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签