1

I have binary data in my SQL Server, I want to read that binary data and display in browser as this binary data is a PDF file - how can this be done?

Here is my database structure:

database

I am retrieving other data from database successfully as shown in this screenshot:

data

What I want is when user click on view button, binary data from database should read and display new webview

        <ListView.ItemTemplate>

            <DataTemplate>
                <ViewCell>
                    <StackLayout Orientation="Horizontal" Padding="12,6">

                    <Label Text="{Binding ReportName}" FontSize="24" 
               Style="{DynamicResource ListItemTextStyle}" />

                    <Label Text="{Binding Date}"  FontSize="18" Opacity="0.6"
               Style="{DynamicResource ListItemDetailTextStyle}"/>

                    <Button Clicked="ShowPDF" Text="View" CommandParameter="{Binding FileContent}"></Button>
                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>

    </ListView>
4
  • 1
    Possible duplicate of Download and Save PDF for viewing
    – EvZ
    Commented Apr 17, 2018 at 5:01
  • its not helpful as its showing with localfile how to download, but i have binary data in database and i need to download from there Commented Apr 17, 2018 at 5:21
  • Your application should talk to a backend and consume the DB data via API, which should return a PDF on a specific endpoint URL.
    – EvZ
    Commented Apr 17, 2018 at 5:25
  • if i upload my whole code can you help me with that? as i already retrieving data through web api, but dont know how i can return pdf from api and work on xamarin Commented Apr 17, 2018 at 5:29

1 Answer 1

1

You first need to retrieve the binary data and content type from the database ,for which you can either use ado.net or entity framework which ever you prefer, then you can convert that binary data to your preferred format and return the web view

         byte[] bytes;
        string contenttype;

        string connectionstring = @"Data Source=localhost\SQLEXPRESS;" + "Initial Catalog=foo_database; Integrated Security=SSPI";

        SqlConnection myconnection = new SqlConnection();

        myconnection.ConnectionString = connectionstring;

        string cvsql = "select binarydata,contenttype from customer where customer_id='1'";

        SqlCommand mycommand = new SqlCommand(cvsql, myconnection);

        SqlDataReader myreader;

        try
        {
            myconnection.Open();
            myreader = mycommand.ExecuteReader();

            myreader.Read();

            bytes = (byte[])myreader["binarydata"];
            contenttype = myreader["contenttype "].ToString();

            Response.Clear();
            Response.Buffer = true;
            Response.Charset = "";
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ContentType = contenttype;
            //Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
            Response.BinaryWrite(bytes);
            Response.Flush();
            Response.End();
   }

   catch{

   }
3
  • thanks for reply abdul rehman, but how it can be done with xamarin as i am consuming data from web api. Do you want me to upload code, so you will get clear picture Commented Apr 17, 2018 at 6:57
  • You can use WebView Control in Xamarin Forms to to load your binary data to specific format Commented Apr 17, 2018 at 7:50
  • i dont know how really Commented Apr 17, 2018 at 8:29

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.