C# 如何从SQL Server读取二进制数据并在浏览器中显示

C# 如何从SQL Server读取二进制数据并在浏览器中显示,c#,xamarin,xamarin.forms,C#,Xamarin,Xamarin.forms,我的SQL Server中有二进制数据,我想读取该二进制数据并在浏览器中显示,因为该二进制数据是一个PDF文件-如何做到这一点 以下是我的数据库结构: 我正在从数据库中成功检索其他数据,如此屏幕截图所示: 我想要的是,当用户单击view按钮时,数据库中的二进制数据应该读取并显示新的webview <ListView.ItemTemplate> <DataTemplate> <ViewCel

我的SQL Server中有二进制数据,我想读取该二进制数据并在浏览器中显示,因为该二进制数据是一个PDF文件-如何做到这一点

以下是我的数据库结构:

我正在从数据库中成功检索其他数据,如此屏幕截图所示:

我想要的是,当用户单击view按钮时,数据库中的二进制数据应该读取并显示新的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>

首先需要从数据库中检索二进制数据和内容类型,您可以使用ado.net或您喜欢的实体框架,然后可以将二进制数据转换为您喜欢的格式并返回web视图

         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{

   }

它的可能副本没有帮助,因为它显示了本地文件如何下载,但我在数据库中有二进制数据,我需要从那里下载。您的应用程序应该与后端对话,并通过API使用DB数据,API应该返回特定端点URL上的PDF。如果我上载我的整个代码,您能帮我吗?由于我已经通过web api检索了数据,但不知道如何从api返回pdf并使用xamarin感谢abdul rehman的回复,但在我使用web api的数据时,如何使用xamarin。你想让我上传代码,这样你会得到清晰的图片吗?你可以使用Xamarin表单中的WebView控件将二进制数据加载到特定的格式中。我不知道到底是怎么回事