Download Attachment from GridAttachmentColumn

6 posts, 1 answers
  1. Richard Weeks
    Richard Weeks avatar
    95 posts
    Member since:
    Oct 2009

    Posted 29 Sep 2011 Link to this post

    I followed the example online to download the attachment from a grid with a GridAttachmentColumn but I can't get the download working. The postback fires, ajax disables but the file is not sent. I am using Entity Framework.

    <telerik:RadGrid 
        AutoGenerateColumns="False" 
        AutoGenerateDeleteColumn="True" 
        AutoGenerateEditColumn="True" 
        DataSourceID="EntityAttachmentsDataSource" 
        ID="EntityAttachmentsGrid" 
        OnDeleteCommand="EntityAttachmentsGrid_OnDeleteCommand" 
        OnInsertCommand="EntityAttachmentsGrid_OnInsertCommand" 
        OnItemCommand="EntityAttachmentsGrid_OnItemCommand"
        OnUpdateCommand="EntityAttachmentsGrid_OnUpdateCommand" 
        runat="server">
        <MasterTableView 
            DataKeyNames="Id" 
            DataSourceID="EntityAttachmentsDataSource">
            <Columns>
                <telerik:GridBoundColumn 
                    DataField="Id" 
                    DataType="System.Int32" 
                    HeaderText="Id" 
                    ReadOnly="True" 
                    UniqueName="Id">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn 
                    DataField="FileName" 
                    HeaderText="FileName" 
                    UniqueName="FileName" 
                    ReadOnly="true" 
                    DataType="System.String">
                </telerik:GridBoundColumn>
                <telerik:GridAttachmentColumn 
                    DataSourceID="EntityAttachmentDataSource"
                    MaxFileSize="1048576" 
                    EditFormHeaderTextFormat="Upload Attachment:"
                    HeaderText="Attachment"
                    HeaderTooltip="Download Entity Attachment"
                    AttachmentDataField="Attachment" 
                    AttachmentKeyFields="Id"
                    FileNameTextField="FileName" 
                    DataTextField="FileName"
                    UniqueName="InfringmentAttachment">
                </telerik:GridAttachmentColumn>
            </Columns>
        </MasterTableView>
        <ClientSettings>
            <ClientEvents OnCommand="GridOnCommand" />
        </ClientSettings>
    </telerik:RadGrid>
      
    <asp:EntityDataSource 
        ConnectionString="name=MyEntities" 
        DefaultContainerName="MyEntities" 
        EnableFlattening="False" 
        EntitySetName="EntityAttachments" 
        EntityTypeFilter="EntityAttachment" 
        ID="EntityAttachmentsDataSource" 
        runat="server">
    </asp:EntityDataSource>
      
    <asp:EntityDataSource 
        ConnectionString="name=MyEntities" 
        DefaultContainerName="MyEntities" 
        EnableFlattening="False" 
        EntitySetName="EntityAttachments" 
        EntityTypeFilter="EntityAttachment" 
        ID="EntityAttachmentDataSource" 
        runat="server" 
        Where="it.[Id] = @Id"
        Select="it.[Attachment]">
        <SelectParameters>
            <asp:Parameter Name="Id" Type="Int32" />
        </SelectParameters>
    </asp:EntityDataSource>

    I disabled ajax fine (ConditionalPostback, etc.) and can upload no worries. But I can't get the attachment to download direct from the datasource. All the examples I can find are for SqlDataSource.

    Richard

  2. Richard Weeks
    Richard Weeks avatar
    95 posts
    Member since:
    Oct 2009

    Posted 02 Oct 2011 Link to this post

    The benefit of a weekends rest allowed me to fix my issue regarding downloading but stay with me!

    I can't work out how to grab the filename of the uploaded file. Here is some code:

    var gridAttachmentColumnEditor = ((GridAttachmentColumnEditor)editedItem
    .EditManager.GetColumnEditor(
    "Attachment"));
      
    attachment.Attachment = gridAttachmentColumnEditor.UploadedFileContent;
      
    attachment.FileName = gridAttachmentColumnEditor.????;

    If I can get this, I am done with implementing this useful feature!

    Richard
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Answer
    Veli
    Admin
    Veli avatar
    2002 posts

    Posted 03 Oct 2011 Link to this post

    Have you tried:

    gridAttachmentColumnEditor.RadUploadControl.UploadedFiles[0].FileName

    Veli
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
  5. Richard Weeks
    Richard Weeks avatar
    95 posts
    Member since:
    Oct 2009

    Posted 03 Oct 2011 Link to this post

    Thanks for your reply. There were a combination of factors that meant the uploaded file was failing. Once I rebuilt my SQL Server table, I can then have access to the filename. In the end I used:

     

    gridAttachmentColumnEditor.RadUploadControl.UploadedFiles[0].GetName();

    I'll need to rework my code a bit to ensure the UploadedFileContent matches what is supposed to be in my database table.

    Richard

  6. Bill
    Bill avatar
    26 posts
    Member since:
    Jun 2008

    Posted 21 Aug 2012 Link to this post

    Hello Richard,

    Can you provide a way so I can download your sample?
    Thanks,
    Bill..
  7. Richard Weeks
    Richard Weeks avatar
    95 posts
    Member since:
    Oct 2009

    Posted 23 Aug 2012 Link to this post

    Hi, I'm really sorry but I don't have a sample as this code forms part of a very large project I worked on. Unfortunately my employer takes a proprietary stance on source code, so I can't copy it out to post here :(

    Richard
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017