Может ли $ .getJSON () получить PDF / Docx в диалоге сохранения - PullRequest
0 голосов
/ 29 июля 2010
 $.getJSON( 
     dUrl,
     data:'{ a: 2, b: 3 }',
     function(data){alert(data);}
      });

Будет ли возможность выполнения запроса $ .getJSON () вызвать pdf / docx в новом окне?если да, не могли бы вы поделиться более подробной информацией об этом ..

Я получаю файл pdf / docx из потока ответов в Fiddler.Но нужно найти способ выдвинуть его в диалоговое окно Сохранить как .

Любая помощь приветствуется ...

Вот пользовательский 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);
            }
        }
}

Действие контроллера:

 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 ]

1 голос
/ 30 июля 2010
$("#frmdownloadDocuments").attr('action',dUrl);
$("#frmdownloadDocuments").submit();

Простая отправка формы от по этой ссылке возвращает мне ответ в виде диалога загрузки.

Я не должен был использовать $.getJSON() запрос на скачивание PDF.

1 голос
/ 29 июля 2010
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...