在 DB 的 GridView 中显示图像,其中 PAth 在其他驱动器中

Display Image in GridView from DB where PAth is in other Drive

我想根据用户 ID 在网格视图中显示图像。问题是我的图像存储在另一个驱动器中,而不是网站文件夹中。即使我得到了路径,也不允许我显示图像。

据此linkHow to display image which is stored in local drive?

在 post 中,答案建议创建另一个页面,根据 ID 传递查询字符串或其他内容。

我是新手,不知道如何使用查询字符串。

我写了下面的代码:-

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
         <Columns>
             <asp:BoundField DataField="User_ID" HeaderText="User_ID" />
             <asp:ImageField DataImageUrlField="Photo_File_Path" HeaderText="Photo_File_Path">
             </asp:ImageField>
         </Columns>
     </asp:GridView>


SqlConnection con = new SqlConnection(CS);
        SqlCommand cmd = new SqlCommand("SELECT User_ID, Photo_File_Path FROM User_M", con);
        con.Open();

        SqlDataReader rdr = cmd.ExecuteReader();
        GridView1.DataSource = rdr;
        GridView1.DataBind();
        con.Close();

如何让我的代码与查询字符串一起工作,或者如果有任何其他方式我可以显示图像。

谢谢。

我不知道这是否仍然适用,但这里是:

您的网络服务器 运行 在一个用户帐户下,您计算机上的所有服务也是如此。它通常是像 IUSR 之类的帐户名。

在您拥有文件的驱动器上,您可以右键单击该文件夹并通过访问权限允许该用户进行读取访问。

当然,这仅适用于您可以自行控制的服务器,不会扩展到 Azure 或其他托管环境。但它应该可以解决你的问题,前提是它仍然是处理这个确切问题的方式。

编辑:您链接的 post 已有 5 年历史。我会谨慎地完全信任它:)

虽然不能真正回答您的具体问题,但您可能想看看在 IIS 中设置一个虚拟目录,以允许您将计算机上任何位置的目录映射到例如您站点的子目录.

假设您的网站位于 C:\sites\mySite,您可以将目录 D:\data\images\ 映射到 C:\sites\mySite\userImages,这样您的网站就可以通过 http:\mysite.com\userImages 引用它].

这可能是组织网站结构的一种更简单的方法,您可以移动该物理目录而无需更改网站代码。

希望你能以任何方式进行工作。 :)

编辑: 此 link 提供了您进行设置所需的信息: https://www.iis.net/configreference/system.applicationhost/sites/site/application/virtualdirectory