【问题标题】:Download file from server controller action using $.post method on client side在客户端使用 $.post 方法从服务器控制器操作下载文件
【发布时间】:2021-09-22 05:53:03
【问题描述】:

我正在使用 Asp.NET Core 5(也使用 DevExtreme),并且我有一个网格,其中包含我想按下按钮并使用此 ID 下载文件的 id 列表

我有一个按钮

   columns.Add().CellTemplate(new JS("editEntity"))
                                       .FormItem(f => f.Visible(false))
                                       .Width(130)
                                       .Alignment(HorizontalAlignment.Center)
                                       .Caption("Download the file"); 

和 JavaScript 方法一样

function editEntity(container, options) {
        $('<div class="btn-dxButton show"></div>').dxButton({
            text: "Download the file",
            type: "default",
            onClick: function (e) {
                $.post('@Url.Action("Download", "ContName")',
                    { fileId: options.data.Id }, function (data) {
                        alert(data);
                        alert(data.FileDownloadName);
                        //download(data.FileDownloadName,data)
                  });
            }
        }).appendTo(container);
    }

我的控制器动作是

public FileResult Download(string Id)
{
     string FilePath = $"{Path}{fileId}.txt";

     var f = File(FilePath, "application/octet-stream", Path.GetFileName(FilePath));

     return f;
}

网格看起来像这样:

我不知道如何在客户端下载此文件... 有可能吗?

非常感谢

【问题讨论】:

    标签: c# razor server asp.net-core-5.0


    【解决方案1】:

    如果您将下载操作设为“获取”,则下载 URL 的超链接就足够了。确保设置正确的 mime 类型。您将不需要“onclick”处理程序。只需插入一个按钮样式的锚:

    <a href="your download url" class="your button styled class... />
    

    【讨论】:

    • 谢谢。我有异常为 FileNotFoundException:找不到文件:{Path}.txt 但是当我将 {Path}.txt 手册复制到我的文件资源管理器时,它找到了这个文件
    猜你喜欢
    • 1970-01-01
    • 2017-02-19
    • 2022-01-13
    • 1970-01-01
    • 2016-02-08
    • 1970-01-01
    • 2020-11-10
    • 2011-08-15
    • 2021-08-21
    相关资源
    最近更新 更多