简体   繁体   English

使用POST FORM / AJAX下载文件?

[英]Download File using POST FORM/AJAX?

I need to download a CSV file for my respective table in a page. 我需要在页面中为各自的表格下载CSV文件。 I don't need to do any action after Download, just that he says Export and the data gets downloaded into CSV file. 下载后,我不需要执行任何操作,只需他说“导出”即可将数据下载到CSV文件中。 The back end code is ready, but how do I do it using POST. 后端代码已经准备好了,但是如何使用POST来完成。

I've referred many suggestions, and tried using window.open but POST seems to be a problem there. 我已经提出了很多建议,并尝试使用window.open,但是POST似乎是一个问题。 Finally tried with FORM creation, but now the issue is I get 终于尝试了FORM创建,但是现在问题是

"415 unsupported media type"

My Request header 我的请求标头

Accept  text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Connection  keep-alive
User-Agent  Mozilla/5.0 (Windows NT 6.1; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0
Content-Length  112
Content-Type    application/x-www-form-urlencoded

Now what I understand is to fix this my content type should be set to JSON, and that's not possible in FORM POST? 现在,我了解的是要解决此问题,我的内容类型应设置为JSON,而在FORM POST中是不可能的?

1) Do we have a better way to Download a file (For a Single page APP using HTML and JS)? 1)我们是否有更好的方法来下载文件(对于使用HTML和JS的单页APP)? NO Library please, plain Javascript Way, getting approval for library is not possible in my project. 请以纯Javascript的方式使用NO库,在我的项目中无法获得库的批准。

2) How can I fix the above issue, If not what are my other options? 2)如何解决以上问题,如果没有,我还有其他选择吗?

Code : 代码:

    var form = document.createElement("form");
    form.id = formId;
    form.action = url;
    form.method = method || "POST";
    form.target = target || "_self";
    if (data) {
        for (var key in data) {
            var input = document.createElement("textarea");
            input.name = key;
            input.value = typeof data[key] === "object" ? JSON.stringify(data[key]) : data[key];
            form.appendChild(input);
        }
    }
    form.style.display = 'none';
    $('#' + containerId).append(form);
    if (!self.isEmptyObject(data)) {
        form.submit();
    }

The fix was required from REST End point REST端点需要此修复程序

Include below Code 包含以下代码

@Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_FORM_URLENCODED})

and changed the params to 并将参数更改为

public javax.ws.rs.core.Response getExceptionDetailLogs(final @FormParam("param1") String param1,
    final @FormParam("param2") String param2) throws Exception {
    ....
}   

Here i have done one example make sure helpful to all : 在这里,我做了一个例子,确保对所有人有帮助:

<form name="myform" id="myform" method="post" enctype= "multipart/form-data">
    <input name="uploadData" id="uploadData" type="file"/>
</form>

$("#uploadData").change(function (e) {
    $('.loading').show();
        var file_data = $('#uploadData').prop('files')[0];
        var form_data = new FormData();
        var csvhtml = '';
        form_data.append('file', file_data);
        $.ajax({
            url: '{{path('bulk_request_update_upload')}}',
            dataType: 'text',
            cache: false,
            contentType: false,
            processData: false,
            data: form_data,
            type: 'post',
            success: function (data) {
        }
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM