Binary Stream from SQL Server

I have gone through https://docs.groupdocs.com/display/viewernet/Working+with+Streams but i still cant figure out how to view binary stream saved in SQL server in a browser. Kindly provide me with sample code as it is urgent and we are getting the license asap. Regards

@kademolaji,

Thanks for your response. First of all, we would like you to know that GroupDocs.Viewer for .NET is a back-end API that provides two modes to render the source document (file or stream), Html based rendering and image based rendering. In Html based rendering, each document page is rendered as an Html page, whereas, in the image based rendering each document page is rendered as an image (png or jpg or bmp file). You can save the output Html pages or the images locally and display them in the browser using your application.

Following is a simple code snippet that gets the image representation of the document from the stream and you can save the images locally to display them in the browser using your application.

ViewerConfig config = new ViewerConfig();
config.StoragePath = “D://storage//”;
config.UseCache = true;

Stream FileStream; //copy the byte array from database into the stream object

ViewerImageHandler imageHandler = new ViewerImageHandler(config);

List<PageImage> AllPages = imageHandler.GetPages(FileStream);

foreach (PageImage image in AllPages)
{
//you can save each image locally using image.Stream
}

We would recommend you to please download our Example project to evaluate all the features of the API. Also, we have developed simple document viewer application (download) for users that will demonstrate how the API works when using it in a front end application.

Furthermore, listed below are the open-source document viewer applications that are developed using GroupDocs.Viewer for .NET API. You can download, use and modify these applications as per your requirements.

In case, you would have any other question or query, please feel free to let us know.

Thanks for the response, we understand your explanation but we need a system where there will be no saving of the image locally. The storage server will keep growing and it will affect the performance of the application. Our requirement is to display the document from the database in a browser without saving anything locally. Any suggestion?

@kademolaji,

Yes, you can achieve this functionality by using Html based rendering. When rendering the document in Html based rendering, the API enables you to get the Html Content of each document page as a string. You can simply display that Html Content on the web page without saving it locally.

Please download this sample application (1.3 MB)
which gets the Html representation of a document and displays it in the browser without saving. The application is currently working with the document that is physically stored in App_Data directory. However, you can change it to work with Stream by using an overloaded HtmlHandler.GetPages(Stream, HtmlOptions) function. Please don’t forget to restore NuGet packages of the sample application. Hope it helps.

Thanks it working

@kademolaji,

You are always welcome.

hi i am stuck in same project which @kademolaji talking about, can i get the above sample? as i am unable to download it

@veezo2007pk,

Thanks for posting to our forum.

You can download the sample project which is provided in one of the above posts from here.

i am not able to download, its says “Sorry, this file is private. Only visible to topic owner and staff members.”

@veezo2007pk,

Thanks for your response. In that case, please try to download the sample application from here.

i want to display pdf and docx in browser or in div. I have done, in which i could display pdf file, but docx file doesn’t work with this way

Controller

 private DiagnosticDetailModel GetFileList(int id)
        {
            var DetList = db.DiagnosticDetailModels.Where(p => p.Id == id).FirstOrDefault();
            return DetList;
        }
 [HttpGet]
        public ActionResult RetrieveFile(int id)
        {
            DiagnosticDetailModel diagnosticDetailModel = GetFileList(id);
            byte[] img = diagnosticDetailModel.FileContent.ToArray();
            Response.AppendHeader("Content-Disposition", "inline");
            return File(img, "application/pdf");

        }

Index.cshtml

<table id="Jdatatable" class="table css-serial" style="  border: 1px solid #5bc0de; border-radius:5px; box-shadow: 2px 2px black;">
    <thead style="background-color:#5bc0de; color:white;font-size:14px;">
        <tr>
            <th>#</th>   
            <th>
                Diagnostic Name
            </th>
            <th>
                Registration Date
            </th>               
        </th>
            <th>View Report</th>
        </tr>
    </thead>
    <tbody style="font-size:12px;">
        @foreach (var item in Model)
        {
        <tr>
            <td></td>


            <td>
                @Html.DisplayFor(modelItem => item.DiagnosticName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Date)
            </td>               
            <td>

               @Html.ActionLink("View", "RetrieveFile", new { id = item.Id }, new { @class = "btn btn-primary btn-sm fa fa-eye", @style = "color:white;background-color:#5bc0de;", @target = "_blank" })                                
            </td>
        </tr>
        }
    </tbody>
</table>

I have database in which i am saving files in binary format and i want to retrive that binary format and display, the code i have shared is working with pdf file, but i want to display both pdf or docx, as i have both files in my database
can you help with groupdoc how can i use with my current project?

@HishamIsmail,

Thanks for your response.

In fact, GroupDocs.Viewer is a stand-alone, backend document rendering API that enables you to render the document in the form that can be easily displayed in your front end application. The API provides Html based rendering and image-based rendering.

  • Html based rendering: In Html based rendering, the API generates the Html representation of each page of the source document. You can easily manipulate the Html content of each page and save it as an Html file. For details, please visit Rendering Documents as HTML.
  • Image-based rendering: In image-based rendering, the API converts each page of the document into an image (PNG, JPG, BMP). You can easily save the image to your desired location and display it in your application to view the content of the document. For details, please visit Rendering Documents as Image.

GroupDocs.Viewer also provides rendering documents from the stream which is also required in your scenario. To learn about how to render document from a stream, please visit this documentation article.

We have also developed open source document viewer applications (listed below) using GroupDocs.Viewer for .NET API. We recommend you to try them.

can i have simple code in which i get binary data from database and display in view? and doc could be pdf or docx

@veezo2007pk,

Thanks for coming back to us.

You can achieve your required functionality using GroupDocs.Viewer following below steps.

  • Get binary data from the database
  • Get binary data into a Stream object
  • Render stream into HTML or image using GroupDocs.Viewer
  • Display the HTML or image in your view

Following is the sample code that may help you display a document in your application.

 private DiagnosticDetailModel GetFileList(int id)
        {
            var DetList = db.DiagnosticDetailModels.Where(p => p.Id == id).FirstOrDefault();
            return DetList;
        }
 [HttpGet]
        public ActionResult RetrieveFile(int id)
        {
             DiagnosticDetailModel diagnosticDetailModel = GetFileList(id);
             byte[] img = diagnosticDetailModel.FileContent.ToArray();
            
             // Convert the img byte array to Stream (you can use any other suitable method as well)
             Stream stream = new MemoryStream(img);

             // Render document using GroupDocs.Viewer 
             ViewerConfig config = new ViewerConfig();
             config.StoragePath = "<your storage folder's path>"
             config.CachePath = "<your cache folder's path>"
             config.UseCache = true;

             String HtmlContent = "";
            
             // Set HTML options
             HtmlOptions options = new HtmlOptions();
             options.IsResourcesEmbedded = true;

             ViewerHtmlHandler handler = new ViewerHtmlHandler(config);

              // Get HTML representation of the document pages
             List<PageHtml> pages = handler.GetPages(stream, options); 
           
             // Get HTML content
             foreach (PageHtml page in pages)
             {
                 HtmlContent += page.HtmlContent;
             }
           
             // Here you can use HtmlContent and send it to your view, i.e. embed it inside a div or so. . .
             // You can also manipulate and display the HTML content page by page using page.HtmlContent 
        }

Hope it helps.

ok i did exactly as you said, but i am still confuse how i can return view for example i did like this
[HttpGet]
public ActionResult RetrieveFile(int id)
{
DiagnosticDetailModel diagnosticDetailModel = GetFileList(id);
byte[] img = diagnosticDetailModel.FileContent.ToArray();

         // Convert the img byte array to Stream (you can use any other suitable method as well)
         Stream stream = new MemoryStream(img);

         // Render document using GroupDocs.Viewer 
         ViewerConfig config = new ViewerConfig();
         config.StoragePath = "<your storage folder's path>"
         config.CachePath = "<your cache folder's path>"
         config.UseCache = true;

         String HtmlContent = "";
        
         // Set HTML options
         HtmlOptions options = new HtmlOptions();
         options.IsResourcesEmbedded = true;

         ViewerHtmlHandler handler = new ViewerHtmlHandler(config);

          // Get HTML representation of the document pages
         List<PageHtml> pages = handler.GetPages(stream, options); 
       
         // Get HTML content
         foreach (PageHtml page in pages)
         {
             HtmlContent += page.HtmlContent;
         }
       
         // Here you can use HtmlContent and send it to your view, i.e. embed it inside a div or so. . .
         // You can also manipulate and display the HTML content page by page using page.HtmlContent 

return dvHtmlContent.InnerHtml = HtmlContent; //its not working like this and i couldnt find dvHtmlContent anywhere kindly help me with this will be appriciate

    }

@veezo2007pk,

Thanks for your response.

We have prepared a simple document viewer for you in ASP.NET MVC using GroupDocs.Viewer and you can download it from here. Please note that the sample application is rendering the document that is located in the App_Data folder. You can add the code to get the document from the database and use the stream object to render the document as described in the previous post. Hope it helps.
.

same issue i am not able to download - same message “Sorry, this file is private. Only visible to topic owner and staff members.”

@veezo2007pk,

Please download the sample application from here.

thanks for your help i have another problem the version i am using for groupdoc is very old its 2.7.0.0. How i can implement same problem with this version?

@veezo2007pk,

Thanks for coming back to us.

You are using a very old version of GroupDocs.Viewer which is now obsolete and not supported anymore. We recommend you to please migrate to the next generation GroupDocs.Viewer API. Please note that the next generation API (starting from v3.0.0) is completely different from the older versions and front-end UI has been disintegrated from the back-end API. Now, you will have to create your own front end application that will be used to display the HTML pages or images rendered by the back-end GroupDocs.Viewer API.

We recommend you to please have a look at the documentation of the next generation API. To explore and evaluate the features of the API please download the examples project.