Click here to Skip to main content
15,567,221 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello

I am trying to pass a base64 string or byte array from a URL action to a controller. Im unsure if I'm going about this the right way so ill explain what I am trying to achieve.

The application will have an option to allow users to preview a file in the file system. I am using a plug in called GroupDocs.Conversion which will flatten any file format to a pdf. I am then using this PDF to populate an Iframe.

Originally I was using a URL action to populate the iframe by passing in the file name and returning the FileResult.All of this was working fine but depending on the file size and type it can take a while to load the preview so I wanted to have a loading spinner.

To do this I started a spinner and then passed the file name with Ajax to the controller to flatten the pfd and return bytes/base64. During this process, the spinner would be loading as this is what can take a while. On return, it would use a URL action to populate the iframe with the returned data.

My issue is whenever I try to call the URL action it says it cannot find the controller/action

What I have tried:

$('.loader').show()

              $.ajax({
                  type: "POST",
                  url: "/Home/DownloadDocument/",
                  data: '{"caseNumber":"' + casenum + '","fileName":"' + name + '"}',
                  contentType: "application/json; charset=utf-8",
                  dataType: "json",
                  success: function (response) {
                      alert(encodeURIComponent(response.file))
                      $('#previewFrame').attr('src', '@Html.Raw(@Url.Action("ViewDocument","Home"))?documentBase=' +encodeURIComponent(response.file) + '&fileName=' + name);

                      $('.loader').hide();
                  }
              });




[HttpPost]
     public JsonResult DownloadDocument(string caseNumber, string fileName)
     {

         var bytes = _file.CreatePDFPreview(_helper.FilePath(caseNumber, fileName));
         string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);

         return Json(new { file = base64String });

     }

     [HttpGet]
     public FileResult ViewDocument(string documentBase, string fileName)
     {
         //Stream stream = new MemoryStream(documentBase);

         return File(documentBase, fileName, "application/pdf");
     }
Posted
Updated 10-Oct-22 5:41am

1 solution

Instead of doing all that (passing a very large base64 string back and forth is quite expensive), why not use jQuery to show/hide the loading icon. Here's an example JSFiddle[^] I put together. Using this method you could eliminate the POST part of the controller and just have the GET method produce the document content direct?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900