C#网格视图文件没有打开

时间:2014-05-17 07:23:55

标签: c# asp.net

我正在将文件上传到我的服务器并将其文件名和文件路径存储在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;
    }

请有人纠正我......我希望用户打开或保存文件

1 个答案:

答案 0 :(得分:0)

// 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

中检索它