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

Problem with radupload inside a radgrid

6 Answers 131 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Arturo
Top achievements
Rank 1
Arturo asked on 06 Sep 2012, 06:43 PM
Hi guys, i'm new with this topic and i need your help.

I know how to edit info inside a grid (now i'm using popup mode) and it works. But my boss asked me to change a field (logo picture converted into base64 string ) and i needed to use the radupload control inside the grid.

I've added it manually and it appears without problems but i don't know how to get this file to convert it into base64 string... could you help me with this? thanks a lot!

This is the code i'm using into the grid
<telerik:GridAttachmentColumn FileName="NuevoLogoFN"
    FilterControlAltText="Filter Nuevo Logo column" HeaderText="Nuevo Logotipo"
    Text="Nuevo Logotipo" UniqueName="NuevoLogo">
</telerik:GridAttachmentColumn>

And this one is part of the codebehind
if (e.CommandName == RadGrid.UpdateCommandName)
      {
 
          //GridDataItem item = (GridDataItem)e.Item;
          GridEditFormItem item = (GridEditFormItem)e.Item;
 
 
          Hashtable newValues = new Hashtable();
 
          item.ExtractValues(newValues);
 
 
         //I need the code here
 
                
          string id = item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ID_Reg"].ToString();

6 Answers, 1 is accepted

Sort by
0
Accepted
Radoslav
Telerik team
answered on 11 Sep 2012, 12:34 PM
Hi Arturo,

To achieve the desired functionality you could try using the following code snippet:
void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
    {
        ...
 
        UploadedFile uploadedFile = ((e.Item as GridEditFormItem)["NuevoLogo"].Controls[0] as RadUpload).UploadedFiles[0];
 
        byte[] bytes = new byte[uploadedFile.InputStream.Length];
        int numBytesToRead = (int)uploadedFile.InputStream.Length;
        int numBytesRead = 0;
        while (numBytesToRead > 0)
        {
            int n = uploadedFile.InputStream.Read(bytes, numBytesRead, 10);
            if (n == 0)
            {
                break;
            }
            numBytesRead += n;
            numBytesToRead -= n;
        }
 
        uploadedFile.InputStream.Close();
 
        string base64String = Convert.ToBase64String(bytes);
    }

Please give it try and let me know if it helps you.

Kind 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
Arturo
Top achievements
Rank 1
answered on 11 Sep 2012, 04:07 PM
It's excellent! thanks a lot!  but now i have another question related to this topic.... how can i detect that this new control (uploadedFile) has a file? this because i wanna validate if the user wants to change the logo o wants to keep the old one.  thanks!
0
Casey
Top achievements
Rank 1
answered on 11 Sep 2012, 04:32 PM
Hi Arturo,

You could modify the solution provided by Radoslav as follows: 

I hope this helps!
Casey

RadUpload uploader = (e.Item as GridEditFormItem)["NuevoLogo"].Controls[0] as RadUpload;
 
 if (uploader.UploadedFiles.Count > 0)
 {
 UploadedFile uploadedFile = uploader.UploadedFiles[0];
 
 byte[] bytes = new byte[uploadedFile.InputStream.Length];
 int numBytesToRead = (int)uploadedFile.InputStream.Length;
 int numBytesRead = 0;
 while (numBytesToRead > 0)
 {
     int n = uploadedFile.InputStream.Read(bytes, numBytesRead, 10);
     if (n == 0)
     {
         break;
     }
     numBytesRead += n;
     numBytesToRead -= n;
 }
 
 uploadedFile.InputStream.Close();
 
 string base64String = Convert.ToBase64String(bytes);
 }
0
Arturo
Top achievements
Rank 1
answered on 11 Sep 2012, 04:45 PM
Thanks for the soon response, it won't worked but if I use  

if ( ((e.Item as GridEditFormItem)["NuevoLogo"].Controls[0] as RadUpload).UploadedFiles.Count > 0)
{
.....
}


it's the way i need to use this control.  Thanks a lot!


[UPDATE]

I forgot something important.... is there anyway to show this encoded image in the radgrid???

0
Radoslav
Telerik team
answered on 13 Sep 2012, 08:38 AM
Hello Arturo,

You could convert the image from base64 string to byte array and use the GridTemplateColumn with RadBinaryImage or GridBinaryImageColumn. I am sending you a simple example which demonstrates the described approach.

I hope this helps.

Greetings,
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
Arturo
Top achievements
Rank 1
answered on 13 Sep 2012, 04:11 PM
I'm pretty confused...how can i do it if i charge the radgrid without codebehind??
Tags
Grid
Asked by
Arturo
Top achievements
Rank 1
Answers by
Radoslav
Telerik team
Arturo
Top achievements
Rank 1
Casey
Top achievements
Rank 1
Share this question
or