Upload a file to Database a simple working example

5 posts, 0 answers
  1. Jaime
    Jaime avatar
    8 posts
    Member since:
    Dec 2012

    Posted 04 Dec 2012 Link to this post


    I have read
    all the threads regarding this issue, but to no avail so I could replicate, including http://www.telerik.com/help/aspnet-ajax/upload-manipulating-files.html demo and I do not understand, I have a database SQL Server 2008, a table with a field called Docs varbinary (MAX), in my application (c# - VS2010) I have a radgrid, use a template field AsyncUpload control, do not know how I have to configure it to save a file to the base data, also not like I have to put in item template to show me the file name and click download, if someone could give me a simple example o explain how to do, greatly appreciate it.

    Ps.: I'm using RadControls for ASP.NET AJAX Q2 2012.

    Thank you.
  2. Jaime
    Jaime avatar
    8 posts
    Member since:
    Dec 2012

    Posted 05 Dec 2012 Link to this post

    Well, i can't upload a file to database but i could to a folder, by now i don't know how to do a button to download.

    I had a table called "Tabla" with 3 fields:
    ImageID    int Key
    ImageName varchar(100) ---> save the file name of the file
    Path varchar(100)-----> save path, folder "Admin/"+filename

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
    <%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <head runat="server">
        <form id="form1" runat="server">
        <asp:ScriptManager runat="server" ID="ScriptManger1">
        <script type="text/javascript">
            function ClientValidationFunction1(source, args) {
                args.IsValid = false;
                var ul = $find(window['UploadId']);
                var inputs = ul.getFileInputs();
                for (i = inputs.length - 1; i >= 0; i--) {
                    if (ul.getFileInputs()[i].value != "") {
                        args.IsValid = true;
            <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateDeleteColumn="True" AutoGenerateEditColumn="True"
                AllowAutomaticInserts="True" GridLines="None" AllowAutomaticDeletes="True" DataSourceID="SqlDataSource1"
                OnInsertCommand="RadGrid1_InsertCommand" CellSpacing="0" Culture="es-ES">
                <MasterTableView AutoGenerateColumns="False" DataKeyNames="ImageID" DataSourceID="SqlDataSource1"
                    CommandItemDisplay="Top" EditMode="PopUp">
    <CommandItemSettings ExportToPdfText="Export to PDF"></CommandItemSettings>
    <RowIndicatorColumn Visible="True" FilterControlAltText="Filter RowIndicator column"></RowIndicatorColumn>
    <ExpandCollapseColumn Visible="True" FilterControlAltText="Filter ExpandColumn column"></ExpandCollapseColumn>
                        <telerik:GridTemplateColumn HeaderText="ImageName" UniqueName="ImageName" DataField="ImageName">
                                <asp:Label runat="server" ID="LblImage" Text='<%# Eval("ImageName") %>'></asp:Label>
                                <telerik:RadUpload ID="RadUpload1" runat="server"
                                <asp:CustomValidator ID="CustomValidator1" runat="server" ClientValidationFunction="ClientValidationFunction1"
                        <telerik:GridTemplateColumn DataField="Path"
                            FilterControlAltText="Filter TemplateColumn column" HeaderText="Path"
                                <asp:Label ID="LblImage1" runat="server" Text='<%# Eval("Path") %>'></asp:Label>
                        <telerik:GridTemplateColumn FilterControlAltText="Filter TemplateColumn1 column"
                                <asp:LinkButton ID="lnkDownload" runat="server"
                                    CommandArgument='<%# Eval("Path") %>' Text="Download"
                        <PopUpSettings Modal="true" Height="500px" Width="400px" />
                        <EditColumn UniqueName="EditCommandColumn1">
    <FilterMenu EnableImageSprites="False"></FilterMenu>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
                DeleteCommand="DELETE FROM [Images] WHERE [ImageID] = @ImageID" InsertCommand="INSERT INTO [Images] ([ImageName],[Path]) VALUES (@ImageName,@Path)"
                SelectCommand="SELECT * FROM [Images]" UpdateCommand="UPDATE [Images] SET [ImageName] = @ImageName WHERE [ImageID] = @ImageID">
                    <asp:Parameter Name="ImageID" Type="Int32" />
                    <asp:Parameter Name="ImageName" Type="String" />
                    <asp:Parameter Name="ImageID" Type="Int32" />

    Default.aspx.cs --->
    using System;
    using System.Web.UI.WebControls;
    using System.Data;
    using Telerik.Web.UI;
    using System.IO;
    using System.Data.Common;
    using System.Data.SqlClient;
    public partial class _Default : System.Web.UI.Page
        protected void Page_Load(object sender, EventArgs e)
            RadGrid1.ItemDataBound += new GridItemEventHandler(RadGrid1_ItemDataBound);
        void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
            if (e.Item is GridEditableItem && e.Item.IsInEditMode)
                RadUpload upload = e.Item.FindControl("RadUpload1") as RadUpload;
                ClientScript.RegisterClientScriptBlock(Page.GetType(), "Upload", string.Format("window['UploadId'] = '{0}';", upload.ClientID), true);
        protected void RadGrid1_InsertCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
            RadUpload upload = e.Item.FindControl("RadUpload1") as RadUpload;
            if (upload.UploadedFiles.Count > 0)
                UploadedFile file = upload.UploadedFiles[0];
                SqlDataSource1.InsertParameters.Add("ImageName", file.GetName());
                SqlDataSource1.InsertParameters.Add("Path", "Admin/" + file.GetName());
        protected void lnkDownload_Click(object sender, EventArgs e)
            WHAT PUT HERE??? 

    lnkDwonload_Click will let me download, but i don's know how, someone can help me pleasee, it's urgent. thankss.
  3. Jaime
    Jaime avatar
    8 posts
    Member since:
    Dec 2012

    Posted 06 Dec 2012 Link to this post

    Well, in the end I could not upload a file to the database, but if I could upload a file to a folder. The problem now is how do I to create a function to download with a link, I have a field that stores the name of the uploaded file and another field which stores the path of the folder on the server + filename, here is my source code. please if someone could give me a help, greatly appreciate it. Thank you.
  4. Jaime
    Jaime avatar
    8 posts
    Member since:
    Dec 2012

    Posted 06 Dec 2012 Link to this post

    I found an example but with a gridview, can anyone help me to use it with RadGrid

    protected void lnkDownload_Click(object sender, EventArgs e)
            LinkButton lnkbtn = sender as LinkButton;
            GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow;
            string filePath = gvDetails.DataKeys[gvrow.RowIndex].Value.ToString();
            Response.ContentType = "application/vnd.ms-excel, application/pdf, application/vnd.ms-excel";
            Response.AddHeader("Content-Disposition", "attachment;filename=\"" + filePath + "\"");
  5. Plamen
    Plamen avatar
    3077 posts

    Posted 07 Dec 2012 Link to this post


    You can refer to this on-line demo where is shown how to upload files with RadAsyncUpload and this demo where is provided on way to download the files.

    Hope this will be helpful.

    Kind regards,
    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.
Back to Top