0

I am using MVC to display the PDF content in View, but it is not rendering properly.

Controller Action:

    [HttpPost]
    public ActionResult GetDocumentContent()
    {
        byte[] result;

        result = System.IO.File.ReadAllBytes(@"C:\test.pdf");

        Response.AppendHeader("Content-Disposition", new System.Net.Mime.ContentDisposition
        {
            FileName = "document.pdf",
            Inline = true,
        }.ToString());

        return  File(result, "application/pdf");
    }

Jquery:

        var url = "/DocumentContent/GetDocumentContent";
        var self = this;
        $.post(url, null,function (data) {
            debugger;
            var obj = $('<object type="application/pdf" width="100%" height="100%" border="2"></object>');
            obj.attr('data', data);
            $('#divContainer').append(obj);              
        });

What is the mistake in this code? How to display the PDF stream in View?

Musa
  • 96,336
  • 17
  • 118
  • 137
user2015534
  • 89
  • 1
  • 1
  • 6

1 Answers1

0

The data attribute is a url not the actual data.
Also Note that the data you're receiving from is text as $.post cannot as yet return a binary result.
One solution would be to change the request type to GET(since you're not posting any data) and just put the url in the object's data attribute

var url = "/DocumentContent/GetDocumentContent";
var obj = $('<object type="application/pdf" width="100%" height="100%" border="2" data="'+url+'"></object>');
$('#divContainer').append(obj); 

EDIT
You can but you'll have to use bare XMLHttpRequest using XHR2, something like.

var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
    if (this.readyState == 4 && this.status == 200){
        var URL = window.URL || window.webkitURL;
        var blobUrl = URL.createObjectURL(this.response);
        var obj = $('<object type="application/pdf" width="100%" height="100%" border="2" data="'+blobUrl+'"></object>');
        $('#divContainer').append(obj); 
    }
}
xhr.open('POST', "/DocumentContent/GetDocumentContent");
xhr.responseType = 'blob';
xhr.send('parameter='+encodeURIComponent(some_data));      
Musa
  • 96,336
  • 17
  • 118
  • 137
  • I need to use the POST method, because i need to send some information from client to server(length > url). Is there any way to bind using POST method? – user2015534 Apr 06 '15 at 22:18