We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

How to load PDF into the PDF Viewer manually?

I'm trying to re implement the choose file from file. by creating a button to select the PDF file then it run a function in the PdfViewerController.cs

public ActionResult Upload()
        {
            string filePath = "";

                HttpPostedFileBase file = Request.Files[0]; //Uploaded file
                int fileSize = file.ContentLength;
                string fileName = file.FileName;
                string mimeType = file.ContentType;
                System.IO.Stream fileContent = file.InputStream;

                file.SaveAs(Server.MapPath("~/") + fileName); //File will be saved in application root

                MemoryStream stream = new MemoryStream();
                file.InputStream.CopyTo(stream);
                PdfRenderer pdfviewer = new PdfRenderer();
                object jsonResult = new object();
                jsonResult = pdfviewer.Load(stream);
                return Content(JsonConvert.SerializeObject(jsonResult));
        }

and on the javascript 

function sendSelectedPDFToServer() {
    var formdata = new FormData(); //FormData object
    var fileInput = document.getElementById('fileInput');
    //Iterating through each files selected in fileInput
    for (i = 0; i < fileInput.files.length; i++) {
        //Appending each file to FormData object
        formdata.append(fileInput.files[i].name, fileInput.files[i]);
    }
    //Creating an XMLHttpRequest and sending
    var xhr = new XMLHttpRequest();
    xhr.open('POST', '/PdfViewer/Upload');
    xhr.send(formdata);
    xhr.onreadystatechange = function () {
        if (xhr.readyState == 4 && xhr.status == 200) {
            alert(xhr.responseText);
            var PDFFile = xhr.responseText
        }
    }
    return false;
}

how can I open the returned PDF from javascript ?


3 Replies

RT Ramya Thirugnanam Syncfusion Team April 23, 2019 07:00 AM UTC

Hi Ahmad, 
 
Thanks for contacting Syncfusion support. 
 
We can pass the PDF document as base64 string to the documentPath property or load() method of PDF viewer control for loading, when the file path is sent dynamically using “Choose from File” button. Please follow the below KB link, 
 
 
In the click event function of the “Choose from File” button, we can pass the file path to the server. In the server, we can convert the PDF document into base64 string with the file path. In the above link, we have provided a web action method GetDocument. In this method, we can pass the file name to the server and get the base64 string to the client. This returned base64 string can be used to load in the PDF viewer control. 
 
Regards, 
Ramya T 



AH Ahmad April 23, 2019 08:10 AM UTC

Thanks. 
it's working now.


RT Ramya Thirugnanam Syncfusion Team April 23, 2019 09:13 AM UTC

Hi Ahmad,  
Thanks for your update. 
Regards,  
Ramya T 


Loader.
Live Chat Icon For mobile
Up arrow icon