用mvc下载文件 [英] Download a file with mvc

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

问题描述

我需要允许我网站上的用户下载文件(xml文件)

I need to allow the user on my site to download a file (xml File)

我尝试了

 public FileResult DownloadFile(string fileid)
 {
     if (!string.IsNullOrEmpty(fileid))
     {            
         byte[] fileBytes = Encoding.ASCII.GetBytes(FileData);
         return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet,FileName + ".bccx");
     }
         return null;
 }

ajax:

function downloadFile(id) {
        $.ajax({
            url: sitePath + "Controller/DownloadFile?fileid=" + id,
            type: 'post',
            asysnc: false
        })
        .done(function () {

        });
}

但没有下载任何内容

推荐答案

是否必须使用Ajax完成?也许您可以打开另一个带有文件生成地址的窗口,然后让浏览器完成该工作:

Does it have to be done using ajax? Maybe you could open another window with the file generation address and let the browser do the job:

function downloadFile(id) {

    window.open(sitePath + "Controller/DownloadFile?fileid=" + id, '_blank');

}

这篇关于用mvc下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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