文件不在asp.net Mvc中下载 [英] File is not downloading in asp.net Mvc

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

问题描述

您好,



我正在实施文件下载功能。一切正常,但不是下载文件在浏览器中打开。下面是我的代码。我创建了一个自定义ActionResult并从控制器调用它。从锚点击事件调用此操作方法。



这是我的ActionMethod



Hello,

I am implementing file downloading feature. every thing works fine but instead of downloading file is opening in browser. below is my code. i created a custom ActionResult and called it from controller. this Action Method is called from a anchor click event.

This is my ActionMethod

public ActionResult DownLoadSubmission(int NodeNo)
      {
          if (NodeNo > 0)
          {
              DataSet ds = DAL.GetDataSet("   select * from Wroks Where NodeNo="+ NodeNo);
              if (ds != null && ds.Tables.Count > 0 && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
              {
                 // DownLoadResult obj = new DownLoadResult(ds.Tables[0].Rows[0]["ActualFileName"].ToString(), ds.Tables[0].Rows[0]["ServerFileName"].ToString());

                  return new  DownLoadResult{FileDownLoadName= ds.Tables[0].Rows[0]["ActualFileName"].ToString(),FileServerName= ds.Tables[0].Rows[0]["ServerFileName"].ToString()};
              }
          }
          return new EmptyResult();
          //return Content("");
      }





这是自定义ActionResult

public class DownLoadResult : ActionResult
{

    public string FileDownLoadName { get; set; }
    public string FileServerName { get; set; }
    public DownLoadResult()
    {

    }
    public DownLoadResult(string orgName, string serverName)
    {
        this.FileDownLoadName = orgName;
        this.FileServerName = serverName;
    }

    public override void ExecuteResult(ControllerContext context)
    {
        string Path = "", PhysicalPath = "";

        Path = context.HttpContext.Request.ApplicationPath;
        PhysicalPath = context.HttpContext.Request.MapPath(Path);
        string strCompletePath = "";
        strCompletePath = PhysicalPath + "Submissions\\" + FileServerName;
        context.HttpContext.Response.AddHeader("content=disposition", "attachment;filename=" + FileDownLoadName);
        context.HttpContext.Response.TransmitFile(strCompletePath);
    }
}





我通过以下锚点击调用Controller ActionMethod 。



I am calling Controller ActionMethod from through following anchor click.

$("#tblWorkList").on("click", "a.download", function (e) {
    debugger
    e.preventDefault();
    var url="@Url.Action("DownLoadSubmission", "Home", new { NodeNo = "-11" })";
    url=url.replace("-11",$(this).attr("data-submissionid"))
    window.location.href = url;

});





我无法下载文件(文件在浏览器中打开),请帮助我..

推荐答案

#tblWorkList).on( 点击 a.download function (e){
调试器
e.preventDefault();
var url = @ Url.Action( DownLoadSubmission 主页 ,new {NodeNo = -11 });
url = url.replace( - 11
("#tblWorkList").on("click", "a.download", function (e) { debugger e.preventDefault(); var url="@Url.Action("DownLoadSubmission", "Home", new { NodeNo = "-11" })"; url=url.replace("-11",


this )。attr( data-submissionid))
window 。< span class =code-sdkkeyword> location .href = url;

});
(this).attr("data-submissionid")) window.location.href = url; });





我无法下载文件(文件在浏览器中打开),请帮助我..


标题应为Content-Disposition而不是content = disposition
The header should be Content-Disposition not content=disposition


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

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