Javascript 在视图中显示AJAX响应文件
我的jquery AJAX是:Javascript 在视图中显示AJAX响应文件,javascript,jquery,asp.net-mvc,html,Javascript,Jquery,Asp.net Mvc,Html,我的jquery AJAX是: $.ajax({ type:'POST', url:'GetFile', date:data, success:function(response){ //How to use the responded file? } }) 在服务器端,我将文件返回为: public ActionResult GetFile() { ReportDocument rd = new ReportDocument(
$.ajax({
type:'POST',
url:'GetFile',
date:data,
success:function(response){
//How to use the responded file?
}
})
在服务器端,我将文件返回为:
public ActionResult GetFile()
{
ReportDocument rd = new ReportDocument();
Stream stream = rd.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
stream.Seek(0, SeekOrigin.Begin);
return new FileStreamResult(stream, "application/pdf");
}
在这里,我从服务器端返回要显示在视图中的文件
也就是说,我只想将响应的文件附加到
<div id="FileContent"></div>
这是我的看法
我能为此做些什么?您想在div中显示PDF 尝试一下:
<div id="FileContent">
<object data="test.pdf" type="application/pdf" width="500" height="800">
</object>
</div>
在ajax调用成功时,使用jquery在#FileContent div中设置
的data属性。这里有一个简单的解决方案。
只需将流写入响应,而不是返回文件
byte[] tempArrary = memoryStream.ToArray();
Response.BinaryWrite(tempArrary);
Response.ContentType = "application/pdf";
Response.End();
然后只返回一些Json结果而不是文件
添加iframe而不是您的DIV
然后使用jquery将iframe的src设置为控制器
$("#your_iframe").attr("src", "your_path");
如果要使用object或Embed标记而不是iframe,则必须为object或Embed创建元素,并执行相同的操作