获取一个可下载的文件与Ajax请求 [英] Getting a downloadable file with an ajax request

查看:95
本文介绍了获取一个可下载的文件与Ajax请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:不知道为什么,这被标记为给原本甚至不真的有一个答案比使用第三方工具,增加了你可能不希望或需要额外的接口的东西等重复。我用ajax的原因是因为我需要发送一组ID,我不知道怎么做比阿贾克斯那个以外。我想通了什么,我需要做的,所以我并不真的需要从这个职位更多的帮助,但是这绝对不是一个重复的问题,即使有,也没有一个真正的答案,原来的。

Not sure why this was marked as a duplicate given that the original doesn't even really have an answer other than "use this third party tool that adds extra interface stuff that you probably don't want or need". The reason I was using ajax is because I need to send an array of ids and I'm not sure how to do it other than with ajax. I figured out what I need to do, so I don't really need any more help from this post, but this definitely isn't a duplicate question, and even if it were, there isn't a real answer to the original.

我一直试图让这将下载我的导出脚本生成的文件的链接。但是,我不能让它得到返回的文件。这是JS的功能我一直在使用...

I've been trying to get a link that will download a file generated by my export script. However, I can't get it to get the returned file. This is the JS function I've been using...

function sendSelected(path) {
    var ids;
    ids = jQuery("#grid").jqGrid('getGridParam', 'selarrrow');
    if (ids.length > 0) {
        alert("");
        $.ajax({
            url: path,
            data: { ids: ids },
            type: "POST",
            traditional: true,
        });
    } else {
        alert("You have not selected any rows.");
        return false;
    }
}

然后这里的HTML部分...

and then here's the HTML portion...

<input type="submit" name="command" value="Export" />
<script type="text/javascript">
    jQuery("#export").click(function () { sendSelected("/Forms/Export"); });
</script>

这一切工作正常,除了我的出口与结束document.Save(回应,file.pdf); 的网页没有得到这回。如果我将其更改为 document.Save(响应,@C:\一些\位置\到\保存\ at.pdf); 文件被创建那里,所以我知道这不是创建文件有问题。另外,如果我只是创建一个指向/表格/导出,然后手动输入id的标准形式,我可以下载文件很好,但不适合我的实施工作。

This all works fine, except my exporter ends with document.Save(response, "file.pdf"); and the page isn't getting this back. If I change it to document.Save(response, @"C:\some\location\to\save\at.pdf"); the file is created there, so I know it's not a problem creating the file. Also, if I just create a standard form that points to /Forms/Export and then manually enter the ids, I can download the file fine, but that doesn't work for my implementation.

推荐答案

$。阿贾克斯是行不通的,我正在尝试类似的东西,但最近结束了不同的解决方案。只要有隐藏输入的表单,然后在sendSelected设置字段和操作,然后提交表单。

$.ajax will not work, i was attempting something similar recently but ended up with different solution. Simply have a form with hidden input and then in your sendSelected set the field and action and then submit form.

    $("#hiddenField").val(data);
    $("#yourForm").attr("action", path)
    $("#yourForm").submit();

这篇关于获取一个可下载的文件与Ajax请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆