文件无法从文件夹下载

时间:2012-05-05 06:15:21

标签: asp.net

我遇到文件下载问题。我有一个网格,显示文件名和下载该文件的链接。 这是我网格的代码。

<asp:GridView ID="gv7" runat="server" AllowPaging="True" CssClass="tbl_green_parrot"
PageSize="5" Width="100%" AutoGenerateColumns="False" EmptyDataText="No Scheme data available" DataKeyNames="FilePath" OnPageIndexChanging="gv7_PageIndexChanging"             onrowdatabound="gv7_RowDataBound">
  <AlternatingRowStyle CssClass="tbl_green_parrot1" />
 <FooterStyle CssClass="tbl_green_parrotfoot" />
 <PagerStyle CssClass="tbl_green_parrotpager" />
 <SelectedRowStyle CssClass="tbl_green_parrotselect" />
 <EmptyDataRowStyle CssClass="tbl_green_parrotempty" />
 <Columns>
   <asp:TemplateField HeaderText="File Name">
   <ItemTemplate>
   <asp:HyperLink ID="lnkFile" Target="_blank" CausesValidation="false" runat="server"
   Text='<%# Bind("DisplayName") %>' NavigateUrl='<%# Bind("FilePath") %>' />
   </ItemTemplate>
   </asp:TemplateField>
   <asp:TemplateField HeaderText="Download" ItemStyle-Width="25px" ItemStyle-HorizontalAlign="Center">
   <ItemTemplate>
   <a id="lnkDLoad" href='<%# Bind("FilePath") %>' runat="server" target="_blank">
   <img src="Images/download.png" alt="Download" />
   </a>
   </ItemTemplate>
   <ItemStyle HorizontalAlign="Center" Width="25px" ></ItemStyle>
   </asp:TemplateField>
 </Columns>

这里我将文件路径绑定到<a>标记。当我将文件放在网站的同一根文件夹中时,此代码正在运行。但是,当我尝试下载文件夹内的文件时,它不会下载。

下载代码:

DataTable new_dt = new DataTable();
    new_dt.Columns.Add("DisplayName");
    new_dt.Columns.Add("FileName");
    new_dt.Columns.Add("FilePath");
    string Query = "SELECT DisplayName,FilePath,Universal From DistScheme";
    dt = DataAccessLayer.DAL.getFromDataTable(Query);
    string filename = "";
    if (dt.Rows.Count > 0)
    {
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            DataRow new_dr = new_dt.NewRow();
            if (dt.Rows[i]["Universal"].ToString() == "False")
            {
                filename = DistributorID + "_" + dt.Rows[i]["FilePath"].ToString();
            }
            else
            {
                filename = dt.Rows[i]["FilePath"].ToString();
            }
            string filepath = Server.MapPath("Uploads/Scheme/") + filename;
            if (File.Exists(filepath))
            {
                new_dr["DisplayName"] = dt.Rows[i]["DisplayName"].ToString();
                new_dr["FileName"] = filename;
                new_dr["FilePath"] = filepath;
                new_dt.Rows.Add(new_dr);
            }
        }
    }
    gv7.DataSource = new_dt;
    gv7.DataBind();

如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

您的文件路径应该从“http”开始。在文件路径中包含完整路径,例如http://domian-name/foldername/filename和extenssion。

答案 1 :(得分:0)

要完成您的任务,即从网格下载文件,您需要在网格中添加以下模板列,如下所示。

<asp:TemplateField HeaderText="File" SortExpression="File Path">
                <ItemTemplate>                       
                    <asp:ImageButton ImageUrl="Images/download.png"  ID="imgbtnDownload" runat="server" CommandName="DOWNLOAD" Text="GetFile" CommandArgument='<%# Bind("FilePath") %>'  ></asp:ImageButton>
                </ItemTemplate>
            </asp:TemplateField>

需要在文件后面的代码中添加如下的Row_command事件。

protected void gv7_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "DOWNLOAD")
    {
    // make sure fileName  contains only file name like 'test.pdf'  
        string fileName = Convert.ToString(e.CommandArgument);

    // make sure filePath  contains file path like 'Uploads/Scheme/test.pdf'        
        string filePath = Server.MapPath(Convert.ToString(e.CommandArgument));

        Response.Clear();
        Response.Buffer = true;

        // Read the original file from disk
        FileStream myFileStream = new FileStream(filePath, FileMode.Open);
        long FileSize = myFileStream.Length;
        byte[] Buffer = new byte[Convert.ToInt32(FileSize)];
        myFileStream.Read(Buffer, 0, Convert.ToInt32(FileSize));
        myFileStream.Close();
        // Tell the browse stuff about the file
        Response.AddHeader("Content-Length", FileSize.ToString());
        //Response.AddHeader("Content-Disposition", "inline; filename=" & fileneme.Replace(" ", "_"))
        Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName + ";");
        //Response.TransmitFile(fileneme)
        //Response.ContentType = getMimeType(sExtention, oConnection)

        // Send the data to the browser
        Response.BinaryWrite(Buffer);
        Response.End();

    }
}

现在点击aspx页面上的图片按钮,你可以获得打开或保存文件的选项。

希望这会对你有所帮助......快乐编码......