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

Save RadBinaryImage to folder on button click.

6 Answers 175 Views
BinaryImage
This is a migrated thread and some comments may be shown as answers.
Cristian
Top achievements
Rank 1
Cristian asked on 06 Sep 2012, 01:12 AM
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.

6 Answers, 1 is accepted

Sort by
0
Casey
Top achievements
Rank 1
answered on 12 Oct 2012, 07:38 AM
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
0
Casey
Top achievements
Rank 1
answered on 12 Oct 2012, 02:09 PM
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();");
    }
}
0
Radoslav
Telerik team
answered on 17 Oct 2012, 06:05 AM
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.
0
Casey
Top achievements
Rank 1
answered on 18 Oct 2012, 08:22 PM
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
0
Shinu
Top achievements
Rank 2
answered on 19 Oct 2012, 07:19 AM
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.
0
Casey
Top achievements
Rank 1
answered on 19 Oct 2012, 10:52 PM
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();
Tags
BinaryImage
Asked by
Cristian
Top achievements
Rank 1
Answers by
Casey
Top achievements
Rank 1
Radoslav
Telerik team
Shinu
Top achievements
Rank 2
Share this question
or