首页 > 解决方案 > 无法下载与 href url 操作一起使用的文件形式的 JavaScript

问题描述

我有一个代码,当我使用锚标记时,当我尝试使用 JQuery 调用按钮单击时,它正在工作,它没有下载文件

function DownloadFile() {
    var data = {
        'tableName': $("#tableName").val(),
        'attachmentId': $("#attachmentId").val()
    };
    $.ajax({
        url: fileUrl,
        type: "POST",
        data: data,
        success: function (res) {
            
        }
    });
}

相同的代码适用于 href 标记上的 url.action,如下所示

<a class="mr-2" href="@Url.Action("Index","Document",new { @tableName = "otherVessel",@attachmentId = otherVessel.AttachmentId})"> @otherVessel.AttachmentName</a>

我的控制器代码如下

public ActionResult Index(string tableName, int attachmentId)
{
   AttachmentRepository attachmentRepository = new AttachmentRepository();
   var result = attachmentRepository.GetDocument(tableName, attachmentId);
   var cd = new System.Net.Mime.ContentDisposition
   {
     FileName = result.AttachmentName,
     Inline = false,
   };
   Response.AppendHeader("Content-Disposition", cd.ToString());
   return File(result.AttachmentData, result.AttachmentType);
}

标签: jqueryasp.net-mvc-4

解决方案


推荐阅读