如何从RadGrid附件列下载二进制数据?

时间:2011-08-23 08:44:30

标签: asp.net download radgrid rad-controls

我的代码

protected void grdFiles_ItemCommand(object source, GridCommandEventArgs e)
      {
          if (e.CommandName == RadGrid.DownloadAttachmentCommandName)
           {

                GridDownloadAttachmentCommandEventArgs args = e as GridDownloadAttachmentCommandEventArgs;
                string fileName = args.FileName;
                int attachmentId = (int)args.AttachmentKeyValues["ProjectFileId"];

                ProTrakEntities objEntity = new ProTrakEntities();
                ProjectFile objFile = (from type in objEntity.ProjectFiles where type.ProjectFileId == attachmentId select type).First();
                string filename = objFile.FileName;
                string Filetype = objFile.FileType;
                byte[] binaryData = (byte[])objFile.FileData;
                //byte[] binaryData = (byte[])data.Tables[0].Rows[0]["BinaryData"];
               // Response.AppendHeader("Content-Disposition", "attachment; filename = "+filename);
               // Response.AppendHeader("Content-Length", filename.Length.ToString());
               //Response.ContentType = Filetype;
               //Response.WriteFile(Server.MapPath("Uploads/"+filename));
               //Response.Flush();
               //Response.Close();
               //Response.End();


               // grdFiles.Items[0].FireCommandEvent(RadGrid.DownloadAttachmentCommandName, parameters);
                Response.Clear();
                Response.ContentType = Filetype;
                Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
                Response.BinaryWrite(binaryData);
                Response.End();

        }
    }

当我调试上传的数据转换为二进制数据并保存在Database.I可以从本地客户端下载。但从服务器端下载时不工作。

1 个答案:

答案 0 :(得分:0)

protected void grdFiles_ItemCommand(object source, GridCommandEventArgs e)
{

    if (e.CommandName == RadGrid.DownloadAttachmentCommandName)
    {
        RadAjaxManager Manager = new RadAjaxManager();
        Manager.EnableAJAX = false;
        GridDownloadAttachmentCommandEventArgs args = e as GridDownloadAttachmentCommandEventArgs;
        string fileName = args.FileName;
        int attachmentId = (int)args.AttachmentKeyValues["ProjectFileId"];

        ProTrakEntities objEntity = new ProTrakEntities();
        ProjectFile objFile = (from type in objEntity.ProjectFiles where type.ProjectFileId == attachmentId select type).First();
        byte[] binaryData = (byte[])objFile.FileData;
        //grdFiles.DataSource = objFile;
        Response.Clear();
        Response.ContentType = "application/octet-stream";
        Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
        Response.BinaryWrite(binaryData);
        Response.Flush();
        Response.Close();
        Response.End();



    }
}

添加此项并确保唯一名称不同。