2014-03-26 131 views
2

我的jQuery AJAX是::顯示Ajax響應文件到視圖

$.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> 

這是爲我的觀點。

我能爲此做些什麼?

回答

0

您想在div內顯示PDF。

試試這個:

<div id="FileContent"> 
    <object data="test.pdf" type="application/pdf" width="500" height="800"> 
    </object> 
</div> 

使用你的Ajax調用成功的jQuery設置你的#FileContent DIV中的<object>的數據屬性。

+0

它不工作 – Rahul

0

在這裏,你有一個簡單的解決方案。 而不是返回文件只是寫入流到響應。

byte[] tempArrary = memoryStream.ToArray(); 
Response.BinaryWrite(tempArrary); 
Response.ContentType = "application/pdf"; 
Response.End(); 

然後只是返回一些Json結果而不是File。

添加iframe放入您的DIV。

然後使用jquery將iframe的src設置爲您的控制器。

$("#your_iframe").attr("src", "your_path"); 

如果你想使用對象或嵌入標籤,而不是iframe中,那麼你必須爲對象或嵌入創建元素和做同樣的