$ .getJSON()可以在保存对话框中获取PDF / Docx

时间:2010-07-29 16:37:00

标签: c# jquery asp.net-mvc json

 $.getJSON( 
     dUrl,
     data:'{ a: 2, b: 3 }',
     function(data){alert(data);}
      });

生成$ .getJSON()请求是否能够在新窗口中弹出pdf / docx?如果是的话,请你分享一下这方面的更多信息..

我从反应流中获取pdf / docx文件到Fiddler。 但需要找到一种方法将其推送到另存为对话框。

感谢任何帮助...

这是Custom ActionResult

public class DownloadResult : ActionResult
    {

        public DownloadResult()
        {
        }
        public DownloadResult(string virtualPath)
        {
            this.VirtualPath = virtualPath;
        }

        public string VirtualPath
        {
            get;
            set;
        }

        public string FileDownloadName
        {
            get;
            set;
        }


        public override void ExecuteResult(ControllerContext context)
        {


            //context.HttpContext.Response.TransmitFile(filePath);
            //context.HttpContext.Response.WriteFile(filePath);
            //context.HttpContext.Response.Flush();
            // Response.BinaryWrite(content)
            //Response.ContentType = "application/msword";
            //Response.ContentType = "application/pdf";
            string filePath = this.VirtualPath;
            if (!string.IsNullOrEmpty(filePath))
            {
                byte[] content = System.IO.File.ReadAllBytes(filePath);
                string contentType = "";
                if (filePath.ToLower().Contains(".pdf"))
                    contentType = "application/pdf";
                else
                    contentType = "application/msword";

                context.HttpContext.Response.Buffer = true;
                context.HttpContext.Response.Clear();
                context.HttpContext.Response.ContentType = contentType;
                if (!string.IsNullOrEmpty(FileDownloadName))
                {
                    context.HttpContext.Response.AddHeader("content-disposition", "attachment; filename=" + this.FileDownloadName);
                }
                context.HttpContext.Response.OutputStream.Write(content, 0, content.Length);
            }
        }
}

Controller的动作:

 public ActionResult DownloadDocument(string uri)
        {
            if (!string.IsNullOrEmpty(uri))
            {

                string targetPath = ConfigurationHelper.GetFolderPath("TempStoreFolder");

                if (string.IsNullOrEmpty(targetPath))
                {
                    targetPath.LogDebug("[TempStoreFolder] setting is not defined in the configuration");
                    return null; 
                }
                uri = Path.Combine(targetPath, uri);

                var downloadResult = new DownloadResult
                {
                    VirtualPath = uri,
                    FileDownloadName = uri
                };
                return downloadResult;
            }
            return null;
        }

2 个答案:

答案 0 :(得分:1)

window.open('http://yoursite.com/my.pdf')

答案 1 :(得分:1)

$("#frmdownloadDocuments").attr('action',dUrl);
$("#frmdownloadDocuments").submit();

简单表单从this Link提交,即可获得“响应为下载”对话框。

我不应该使用$.getJSON()请求下载PDF。