2

I try to open getting file from DB as byte array. That's what i have:

  1. From JavaScript i call method Viewer in controller:

    $("#preview-file").click(function () { $("#preview-pdf").html("");

           $.ajax({
            type: 'GET',
            url: '@Url.Action("Viewer", "Home")?fileId='+fileIdJS,
            success: function (result) {
                $('#preview-pdf').append(result);
            },
            error: function () {
                console.log("Error!");
            }
        });
    });
    
  2. Method Viewer:

    [HttpGet] public ActionResult Viewer(string fileId) { string fileName = string.Empty; byte[] bytes = new byte[] {};

           using (SqlConnection connection = new SqlConnection())
           {
              using (SqlCommand cmdDownload = new SqlCommand())
              {
                  OpenConnection(connection);
                  cmdDownload.Connection = connection;
    
                  cmdDownload.CommandText = "SELECT file_stream, name FROM FileTable WHERE stream_id = @fileId";
                  cmdDownload.Parameters.AddWithValue("@fileId", fileId);
    
                  using (SqlDataReader fileDataReader = cmdDownload.ExecuteReader())
                  {
                      while (fileDataReader.Read())
                      {
                          fileName = (string)fileDataReader["name"];
                          bytes = (byte[])fileDataReader["file_stream"];
                      }
                  }
                  connection.Close();
              }            
          }
    
          ViewBag.b = bytes;
          return PartialView("~/Views/Home/Viewer.cshtml");
      }
    
  3. And Viewer.cshtml (sorry cant past code): http://pastebin.com/peybdTad

I know what pdf.js cant work with byte array. I try conver array in uint8array, but i did not work. Some may suggest how can i open a byte array in pdf.js? I would appreciate any information.

Max Gordey
  • 21
  • 2
  • PDF.js can work with byte array, strings and typed arrays, use them in getDocument overloaded function, e.g. `.getDocument({data: arrOrStr})` – async5 Jan 26 '17 at 14:16
  • Why you cannot pass URL to the getDocument of the handler that will return PDF octet stream? Seems like you are doing the job HTTP transport suppose to do. – async5 Jan 26 '17 at 14:18
  • I followed your advice and overloaded `getDocument`. That's what I got: http://pastebin.com/yas79juH But nothing has changed: http://prntscr.com/e10d18 I think, maybe not enough to specify arrays of data in getDocument, perhaps even need to specify any parameters? – Max Gordey Jan 27 '17 at 12:45
  • I use [FileTable](https://msdn.microsoft.com/en-us/library/ff929144.aspx) and all data save in satabase. Files themselves physically stored in the media and I even know where, but I can not access them (lack of experience). This is the only way I have found (transfer data array) – Max Gordey Jan 27 '17 at 12:49
  • See https://github.com/mozilla/pdf.js/blob/master/examples/learning/helloworld64.html, but looks like jQuery's ajax just corrupts PDF data. You could avoid this issue by just giving PDF.js a URL of octet stream. – async5 Jan 27 '17 at 15:44
  • Also see http://stackoverflow.com/questions/1757799/displaying-a-image-from-a-database-in-asp-net-mvc – async5 Jan 27 '17 at 15:50
  • I've tried everything. The maximum that I got it: [source](http://pastebin.com/XHDB0D3n) , [screenshot](http://prnt.sc/e26ggn). File allegedly opened file (displayed exact number of pages), but the file is displayed. I checked the console and where all the data is transferred correctly, I think the thing in the mapping data, but where I can not identify. – Max Gordey Jan 30 '17 at 11:39
  • Looks like PDF.js works fine. Can you check if all need CSS is included? Use DOM inspector or find canvases and check properties. – async5 Jan 30 '17 at 14:15
  • Yes, all CSS include (viewer.css) and worked fine. І do not use canvas layout, and perhaps this problem? – Max Gordey Jan 31 '17 at 07:37

0 Answers0