Save RadBinaryImage to folder on button click.

7 posts, 0 answers
  1. Cristian
    Cristian  avatar
    46 posts
    Member since:
    May 2011

    Posted 05 Sep 2012 Link to this post

    Hi, Guys I'm new to radbinaryimage contro.
    I've implemented this demo succesfully http://demos.telerik.com/aspnet-ajax/upload/examples/async/ajaxprocessing/defaultcs.aspx?product=asyncupload but now I want to add a "save image" button that saves to a folder  the image that is shown on the radbinaryImage control.
    How can I achieve this?
    Hope your help.
  2. Casey
    Casey avatar
    14 posts
    Member since:
    May 2011

    Posted 12 Oct 2012 Link to this post

    Any updates on this?  I'm wanting to do the same.  Another option for me would be to click the image, have it open in another page and then allow them to save it from there.  I'm using the RadBinaryImage control inside a RadGrid.

    Thanks,
    Casey
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Casey
    Casey avatar
    14 posts
    Member since:
    May 2011

    Posted 12 Oct 2012 Link to this post

    I was able to get it to work using a GridButtonColumn.  Now I just need to figure out how to save the original file name so I can use that when saving the image.  I currently have it hardcoded as "image.jpg".

    <telerik:GridButtonColumn Text="Save" CommandName="Save" ShowInEditForm="false" UniqueName="Save"  >
    </telerik:GridButtonColumn>
    function disableAJAX() {
                    var manager = $find('<%= RadAjaxManager1.ClientID %>');
                    manager.set_enableAJAX(false);
                    setTimeout(function () {
                        manager.set_enableAJAX(true);
                    }, 100);
                }
    protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
    {
        if (e.CommandName == "Save")
        {
            GridDataItem item = e.Item as GridDataItem;
     
            string ImageID = item.GetDataKeyValue("Image_ID").ToString();
            string fileName = "image.jpg"; // item.GetDataKeyValue("Filename").ToString();//.Replace(' ', '_') + ".jpg";
     
            byte[] data = GetCustomerPhoto(ImageID);
     
            Response.Clear();
            Response.ContentType = "image/png";
            Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
            Response.BinaryWrite(data);
            Response.End();
        }
    }
     
    private byte[] GetCustomerPhoto(string ImageId)
    {
        SqlConnection conn = new SqlConnection("yourconnectionstringhere");
        SqlCommand comm = new SqlCommand("SELECT image FROM tblImages WHERE Image_ID = @ID", conn);
        comm.Parameters.Add(new SqlParameter("@ID", ImageId));
     
        conn.Open();
        object data = comm.ExecuteScalar();
        conn.Close();
     
        return (byte[])data;
    }
     
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {

        if (e.Item is GridDataItem)
        {
            GridDataItem item = (GridDataItem)e.Item;
            LinkButton link = (LinkButton)item["Save"].Controls[0];
            int index = e.Item.ItemIndex;
            link.Attributes.Add("onclick", "disableAJAX();");
        }
    }
  5. Radoslav
    Admin
    Radoslav avatar
    1564 posts

    Posted 17 Oct 2012 Link to this post

    Hi Casey,

    If you have the name of the image stored in your database you could add the datatable column's name as a second DataKeyNames parameter of the MasterTableView. For example if the image name is kept into the “ImageName” column you could use the following markup:
    <telerik:RadGrid ID="RadGrid1" runat="server" ..>
        <MasterTableView DataKeyNames="Image_ID, ImageName">
    Then on RadGrid1_ItemCommand you could get the name by calling GetDataKeyValue to the GridDataItem:
    protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
    {
        if (e.CommandName == "Save")
        {
            GridDataItem item = e.Item as GridDataItem;
      
            string ImageID = item.GetDataKeyValue("Image_ID").ToString();
            string fileName = item.GetDataKeyValue("ImageName").ToString();
    Please give it try and let me know if it helps you.

    Looking forward for your reply.

    Regards,
    Radoslav
    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.
  6. Casey
    Casey avatar
    14 posts
    Member since:
    May 2011

    Posted 18 Oct 2012 Link to this post

    I'm using the GridBinaryImageColumn to save the file.  How can I get to the filename from that control so I can write it to the database?

    Thanks,
    Casey
  7. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 19 Oct 2012 Link to this post

    Hi,

    You can access the file name as shown below.
    C#:
    protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
    {
        if (e.CommandName == "Save")
        {
            GridEditableItem editedItem = (GridEditableItem)e.Item;
            RadUpload upload = (RadUpload)((Telerik.Web.UI.GridDataItem)(editedItem)).EditFormItem["Upload"].Controls[0];
            string name = upload.UploadedFiles[0].FileName;
        }
    }

    Thanks,
    Shinu.
  8. Casey
    Casey avatar
    14 posts
    Member since:
    May 2011

    Posted 19 Oct 2012 Link to this post

    Thanks Shinu.  That was close enough to get me there. 

    GridEditableItem editedItem = (GridEditableItem)e.Item;
    GridBinaryImageColumnEditor editor = ((GridEditableItem)e.Item).EditManager.GetColumnEditor("Upload") as GridBinaryImageColumnEditor;
    RadUpload upload = editor.RadUploadControl;
    filename = upload.UploadedFiles[0].GetName();
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017