Can $.getJSON() get PDF/Docx in Save Dialog
$.getJSON(
dUrl,
data:'{ a: 2, b: 3 }',
function(data){alert(data);}
});
Will making a $.getJSON() request be able to pop up the pdf/docx in a new window? if yes, could you please share more info on this..
I'm getting the pdf/docx file from Response stream into Fiddler. But need to find a way to push it to Save As Dialog box.
Any help is appreciated...
Here is the 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's Action :
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;
}
window.open('http://yoursite.com/my.pdf')
$("#frmdownloadDocuments").attr('action',dUrl);
$("#frmdownloadDocuments").submit();
Simple Form Submit from this Link gets me the Response as Download dialog.
I shouldn't have used $.getJSON()
request to download PDF.
精彩评论