This is a migrated thread and some comments may be shown as answers.

how can i download binarydata from server using gridattachmentcolumn?

1 Answer 132 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Tirumalesh
Top achievements
Rank 1
Tirumalesh asked on 23 Aug 2011, 11:34 AM
This is default page code:
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;
             
                Response.Clear();
                Response.ContentType = Filetype;
                Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
                Response.BinaryWrite(binaryData);
                Response.End();
            
        }
    }

**Uploaded file Code**
 protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
    {
        if (DetailsView1.CurrentMode == DetailsViewMode.Insert)
        {
            if (Session["ProjectId"] != null)
            {
                int Projectid = Convert.ToInt32(Session["ProjectId"]);
                string Description = (DetailsView1.FindControl("RadEditor1") as RadEditor).Content;
                RadAsyncUpload radAsyncUpload = DetailsView1.FindControl("RadAsyncUpload1") as RadAsyncUpload;
                UploadedFile file = radAsyncUpload.UploadedFiles[0];
                string s = file.FileName;
                string path = System.IO.Path.GetFileName(s);
                    string Contenttype = radAsyncUpload.UploadedFiles[0].ContentType;
                int fileSize = radAsyncUpload.UploadedFiles[0].ContentLength;
                float length = float.Parse(fileSize.ToString());
                byte[] fileData = new byte[file.InputStream.Length];
                file.InputStream.Read(fileData, 0, (int)file.InputStream.Length);
                        ProTrakEntities objEntity = new ProTrakEntities();
                        ProjectFile objFile = new ProjectFile();
                        objFile.ProjectId = Projectid;
                        objFile.FileName = s;
                        objFile.FileType = Contenttype;
                        objFile.FileSize = length;
                        objFile.CreatedBy = "admin";
                        objFile.CreatedDate = DateTime.Now;
                        objFile.Description = Description;
                        objFile.FileData = fileData;
                        objEntity.AddToProjectFiles(objFile);
                        objEntity.SaveChanges();
                
            }
         }
    }
I'm saving the data in database in binaryfromat.i data was not downloading from sever when i click the attachmentcolumn.

1 Answer, 1 is accepted

Sort by
0
Veli
Telerik team
answered on 26 Aug 2011, 07:35 AM
Hi Tirumalesh,

The sample code you provided seems fine. Can you verify the following things:

1. Response.ContentType contains a valid content type value (e.g. "image/jpeg", "application/octet-stream", etc.)
2. Binary data is retrieved successfully from your data layer and the binaryData variable contains a valid byte array representation of your attachment.
3. Ajax is disabled by the time of the download, if you are using it.

Veli
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

Tags
Grid
Asked by
Tirumalesh
Top achievements
Rank 1
Answers by
Veli
Telerik team
Share this question
or