c#网格视图文件不能打开

本文关键字:不能 文件 网格 视图 | 更新日期: 2023-09-27 18:04:00

我正在上传一个文件到我的服务器,并将其文件名和文件路径存储在SQL数据库中。

我试图在gridview中显示文件…但是打不开

下面是我的代码
<asp:GridView ID="GridView1" runat="server" Width="45%" style="text-align:center;"
        AutoGenerateColumns="false" HeaderStyle-ForeColor="White" DataKeyNames="TASK_ID"
        AllowPaging ="true" emptydatatext="No Attachments" BackColor="AliceBlue" Font-Size = "11pt" AlternatingRowStyle-BackColor = "#F2F2F2" RowStyle-BorderWidth="1" AlternatingRowStyle-BorderWidth="1"  
        HeaderStyle-BackColor = "#00829c" HeaderStyle-BorderColor="#CC9966" HeaderStyle-BorderWidth="1px" HeaderStyle-BorderStyle="Solid">
    <Columns>
        <asp:BoundField DataField="TASK_ID" Visible="false" HeaderText="Id" />
        <asp:BoundField DataField="ATTACH_FILENAME" HeaderText="FileName" />
        <asp:TemplateField HeaderText="View" HeaderStyle-Width="90%">
            <ItemTemplate>
                <asp:HyperLink ID="HyperLink1" Target="_blank" runat="server" Text='<%# Eval("ATTACH_FILENAME") %>'
        NavigateUrl='<%# Eval("ATTACH_FILEPATH") %>'>
                </asp:HyperLink>
            </ItemTemplate>
        </asp:TemplateField> 
    </Columns>
</asp:GridView>
private void Binddata()
{
    string strQuery = "SELECT TASK_ID,ATTACH_FILENAME,ATTACH_FILEPATH from child_taskcreator where TASK_ID ='" + lblTaskid.Text + "'";
    SqlCommand cmd = new SqlCommand(strQuery);
    GridView1.DataSource = GetData(cmd);
    GridView1.DataBind();
}
private DataTable GetData(SqlCommand cmd)
{
    DataTable dt = new DataTable();
    using (SqlDataAdapter sda = new SqlDataAdapter())
    {
        cmd.Connection = connection;
        connection.Open();
        sda.SelectCommand = cmd;
        sda.Fill(dt);
        connection.Close();
        return dt;
    }

请有人纠正我…我想让用户打开或保存文档

c#网格视图文件不能打开

// code to upload files
HttpFileCollection fileCollection = Request.Files;
if (fileCollection.Count != 0)
{
    string filename = string.Empty;
    for (int i = 0; i < fileCollection.Count; i++)
    {
        HttpPostedFile file = fileCollection[i];
        filename = Path.GetFileName(file.FileName);
        if (file.ContentLength > 0)
        {
            string strFilePath = "~/Uploads/";
            System.IO.DirectoryInfo DI = new System.IO.DirectoryInfo(MapPath(strFilePath));
            if (!DI.Exists)
            {
                System.IO.Directory.CreateDirectory(MapPath(strFilePath));
            }
            strFilePath = strFilePath + filename;
            System.IO.FileInfo FI = new System.IO.FileInfo(MapPath(strFilePath));
            if (!FI.Exists)
            {
                file.SaveAs(Server.MapPath(strFilePath));
            }
            // save the filepath variable to your database
        }
    }
}

将filepath变量保存到数据库中,然后它将从gridview

中检索。
相关文章: