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

Upload a file to Database a simple working example

4 Answers 808 Views
AsyncUpload
This is a migrated thread and some comments may be shown as answers.
Jaime
Top achievements
Rank 1
Jaime asked on 04 Dec 2012, 12:36 PM
Hi:

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.

4 Answers, 1 is accepted

Sort by
0
Jaime
Top achievements
Rank 1
answered on 06 Dec 2012, 05:48 AM
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

Default.aspx-->
<%@ 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">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager runat="server" ID="ScriptManger1">
    </asp:ScriptManager>
 
    <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;
                    return;
                }
            }
        }
    </script>
 
    <div>
        <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>
                <Columns>
                    <telerik:GridTemplateColumn HeaderText="ImageName" UniqueName="ImageName" DataField="ImageName">
                        <ItemTemplate>
                            <asp:Label runat="server" ID="LblImage" Text='<%# Eval("ImageName") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <telerik:RadUpload ID="RadUpload1" runat="server"
                                TargetFolder="Admin"
                                OverwriteExistingFiles="true"
                                ControlObjectsVisibility="None">
                            </telerik:RadUpload>
                            <asp:CustomValidator ID="CustomValidator1" runat="server" ClientValidationFunction="ClientValidationFunction1"
                                ErrorMessage="CustomValidator">
                            </asp:CustomValidator>
                        </EditItemTemplate>
                    </telerik:GridTemplateColumn>
                    <telerik:GridTemplateColumn DataField="Path"
                        FilterControlAltText="Filter TemplateColumn column" HeaderText="Path"
                        UniqueName="TemplateColumn">
                        <ItemTemplate>
                            <asp:Label ID="LblImage1" runat="server" Text='<%# Eval("Path") %>'></asp:Label>
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                    <telerik:GridTemplateColumn FilterControlAltText="Filter TemplateColumn1 column"
                        UniqueName="TemplateColumn1">
                        <ItemTemplate>
                            <asp:LinkButton ID="lnkDownload" runat="server"
                                CommandArgument='<%# Eval("Path") %>' Text="Download"
                                onclick="lnkDownload_Click"></asp:LinkButton>
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                </Columns>
                <EditFormSettings>
                    <PopUpSettings Modal="true" Height="500px" Width="400px" />
                    <EditColumn UniqueName="EditCommandColumn1">
                    </EditColumn>
                </EditFormSettings>
            </MasterTableView>
 
<FilterMenu EnableImageSprites="False"></FilterMenu>
        </telerik:RadGrid>
 
        <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">
            <DeleteParameters>
                <asp:Parameter Name="ImageID" Type="Int32" />
            </DeleteParameters>
            <UpdateParameters>
                <asp:Parameter Name="ImageName" Type="String" />
                <asp:Parameter Name="ImageID" Type="Int32" />
            </UpdateParameters>
            <InsertParameters>
            </InsertParameters>
        </asp:SqlDataSource>
    </div>
    </form>
</body>
</html>

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.
0
Jaime
Top achievements
Rank 1
answered on 06 Dec 2012, 05:20 PM
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.
0
Jaime
Top achievements
Rank 1
answered on 06 Dec 2012, 09:11 PM
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 + "\"");
        Response.TransmitFile(Server.MapPath(filePath));
        Response.End();
    }
0
Plamen
Telerik team
answered on 07 Dec 2012, 11:31 AM
Hi,

 
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,
Plamen
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.
Tags
AsyncUpload
Asked by
Jaime
Top achievements
Rank 1
Answers by
Jaime
Top achievements
Rank 1
Plamen
Telerik team
Share this question
or