在当前视图或新视图中下载文件

时间:2020-04-02 08:17:11

标签: asp.net asp.net-mvc asp.net-core

必须下载文件,要么保留在当前页面上,要么打开一个新文件。告诉我该怎么做。这是已经完成的事情。

public FileResult DownloadFile(int id)
        {
            FilesDB filesDB = _context.FilesDB.Find(id);
            filesDB.DownloadCount++;
            _context.SaveChanges();
            string downloadName= filesDB.FileDisplayName +
               Path.GetExtension(filesDB.FilePath);
            return File(filesDB.FilePath, filesDB.FileType, downloadName);            
        }

1 个答案:

答案 0 :(得分:0)

您可以双向下载。只需使用HTML表单即可。

我在这里使用WebService:

    <form id="downloadForm" action="MyService.asmx/DownloadFile" method="post" target="_self" >
        <input type="hidden" name="id" id="txtID" />
    </form>

JS:

function downloadFile() {
    $('#txtID').val(id);
    $('#downloadForm').submit();
}

只需调用downloadFile(),它将下载并停留在当前页面上。 如果您想以新的方式打开,只需将 target =“ _ self” 更改为 target =“ _ blank”