Adem Ayg Adem Ayg - 1 month ago 14
Ajax Question

Failed to load PDF document (ASP.NET MVC ajax)

I need display dynamically pdf. But I get error : Failed to load PDF document.(I use Chrome)

Index.cshtml:

<div >
<h3> AJAX: (data='@@Url.Action("GetPDF")')</h3>
<object data='@Url.Action("GetPDF")' type="application/pdf" width="300" height="200"></object>
</div>
<div>
<h3> PATH: (data="/Pdf/32_1.pdf")</h3>
<object data="/Pdf/32_1.pdf" type="application/pdf" width="300" height="200"></object>
</div>


HomeController.cs:

public FileStreamResult GetPDF()
{
string fileName = "32_1.pdf";
FileStream fs = new FileStream(@"C:\Documents\MUH0000020\" + fileName, FileMode.Open, FileAccess.Read);
return File(fs, "application/pdf", fileName);
}


result :

enter image description here

please help.

Answer

I changed my method :

public FileStreamResult GetPDF()
        {
            string fileName = "32_1.pdf";
            StreamReader reader = new StreamReader(@"C:\Documents\MUH0000020\" + fileName);

            return new FileStreamResult(reader.BaseStream, "application/pdf");
        }
Comments