Upload to DB without temporary folder upload.

11 posts, 0 answers
  1. Dona
    Dona avatar
    23 posts
    Member since:
    Apr 2013

    Posted 04 Aug 2013 Link to this post

    Hi

    I want to use radasyncupload to upload my files to the database. Is it possible without using the temporary folder. The control should not upload files temporarily.

    Thanks
    Dona
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 04 Aug 2013 Link to this post

    Hi Dona,

    It is possible to directly upload files to the database without using the temporary upload folder. For this requirement do not set TemporaryFolder property and set the DisableChunkUpload property to true. Then the files can be directly saved to the DB using a Custom Http Handler. Please have a look into the full code I tried which worked fine at my end.

    ASPX:
    <telerik:RadAsyncUpload runat="server" ID="RadAsyncUpload1" MaxFileSize="57971152"
        DisableChunkUpload="true" HttpHandlerUrl="~/RadAsyncUpload/Handler.ashx" UploadedFilesRendering="BelowFileInput">
    </telerik:RadAsyncUpload>

    C# : (Handler.ASHX)
    public class CustomHandler : AsyncUploadHandler
    {
        protected override IAsyncUploadResult Process(UploadedFile file, HttpContext context, IAsyncUploadConfiguration configuration, string tempFileName)
        {
            System.IO.Stream fileStream = file.InputStream;
            byte[] attachmentBytes = new byte[fileStream.Length];
            fileStream.Read(attachmentBytes, 0, Convert.ToInt32(fileStream.Length));
     
            System.Data.SqlClient.SqlConnection conn = null;
            try
            {
                try
                {
                    conn = new System.Data.SqlClient.SqlConnection(
                        System.Configuration.ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
                    conn.Open();
                    System.Data.SqlClient.SqlCommand insertCommand =
                        new System.Data.SqlClient.SqlCommand(
                        "Insert into [Pictable] (msgid, pic1) Values (1, @Pic)", conn);
                    insertCommand.Parameters.Add("@Pic", System.Data.SqlDbType.VarBinary).Value = attachmentBytes;
                    int queryResult = insertCommand.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                }
            }
            finally
            {
                if (conn != null)
                {
                    fileStream.Close();
                    conn.Close();
                }
            }
            return CreateDefaultUploadResult<UploadedFileInfo>(file);
        }
    }

    Thanks,
    Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Harald Gläser
    Harald Gläser avatar
    3 posts
    Member since:
    Jun 2012

    Posted 21 Oct 2013 Link to this post

    Hallo Shinu,

    I tried your code, and it does not work. But I am not familiar with http handlers. Is anything to do that the handler works (web.config ?)
    My actions:
    I create a new generic handler with vs 12. Paste the code and uncomment the default class "Handler1"

    But the function (Process) you wrote was not called. If I uncomment the default class, the “ProcessRequestwas called.

    What’ wrong / missing?

    Regards

    Harald

  5. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 22 Oct 2013 Link to this post

    Hi Herald,

    There is no need to add any additional settings in the web.config for the Http handler to work. Please note that the class names in the handler should be the same.

    ASPX:
    <telerik:RadAsyncUpload runat="server" ID="RadAsyncUpload1" MaxFileSize="57971152"
        DisableChunkUpload="true" HttpHandlerUrl="~/RadAsyncUpload/Handler.ashx" UploadedFilesRendering="BelowFileInput">
    </telerik:RadAsyncUpload>

    C#: Handler.ashx
    <%@ WebHandler Language="C#" Class="Handler" %>
     
    using System;
    using System.Web;
    using Telerik.Web.UI;
     
    public class Handler : AsyncUploadHandler
    {
        protected override IAsyncUploadResult Process(UploadedFile file, HttpContext context, IAsyncUploadConfiguration configuration, string tempFileName)
        {
            System.IO.Stream fileStream = file.InputStream;
            byte[] attachmentBytes = new byte[fileStream.Length];
            fileStream.Read(attachmentBytes, 0, Convert.ToInt32(fileStream.Length));
     
            System.Data.SqlClient.SqlConnection conn = null;
            try
            {
                try
                {
                    conn = new System.Data.SqlClient.SqlConnection(
                        System.Configuration.ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
                    conn.Open();
                    System.Data.SqlClient.SqlCommand insertCommand =
                        new System.Data.SqlClient.SqlCommand(
                        "Insert into [Pictable] (msgid, pic1) Values (1, @Pic)", conn);
                    insertCommand.Parameters.Add("@Pic", System.Data.SqlDbType.VarBinary).Value = attachmentBytes;
                    int queryResult = insertCommand.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                     
                }
            }
            finally
            {
                if (conn != null)
                {
                    fileStream.Close();
                    conn.Close();
                }
            }
            return CreateDefaultUploadResult<UploadedFileInfo>(file);
        }
    }

    Hope this helps,
    Shinu.
  6. Harald Gläser
    Harald Gläser avatar
    3 posts
    Member since:
    Jun 2012

    Posted 22 Oct 2013 Link to this post

    Hallo Shinu,

    thanks for your response!
    In the meantime I found an complete example in Telerics Demo site with code something to learn. And it works :-)

    But, you’re right there was a different between the ashx and ashx.vb class name.

    Harald

  7. JC
    JC avatar
    30 posts
    Member since:
    Feb 2013

    Posted 05 Nov 2013 Link to this post

    Hello shinu
    I was following the code you posted in this thread to avoid the use of temporary folder. when i tested things are working good but contrary to what you described the temp folder with RadUploadTest is created in app_data but at the same time the uploaded file is available in the http context object. Creating a temp folder and moving file to context object seems confusing. Is this some sort of bug?
  8. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 05 Nov 2013 Link to this post

    Hi JC,

    This is the expected behavior of the control and not a bug. Even if you use any HTTP handler to upload files directly to the target, the control by design always create a TemporaryFolder to verify if it has write permissions on the File System. The Temporary folder will be created but files wont be transferred there. Instead the uploaded file will be available in the HTTP context object of the handler as you mentioned.

    If you would like to have such a functionality that allow the user to determine whether the TemporaryFolder will be created or not, you can submit a new feature request.

    Hope this helps,
    Shinu.
  9. Catalin
    Catalin avatar
    7 posts
    Member since:
    Mar 2011

    Posted 14 Feb 2014 in reply to Shinu Link to this post

    Hi Shinu,
    I don't get it how you consider this a feature and not a bug... There are bunch of behavior properties in this control like EnablePermissionsCheck, UseApplicationPoolImpersonation, DisableChunkUpload which together with HttpHandlerUrl lead you to the ideea that this control can be used without the temp folder. But you there is one small detail...onPreRender you forgot to drop the call for TestTemporaryFolderPermissions method. Isn't stupid to check for some permissions when you don't need to check them? 
    Regards,
    Cata
  10. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 17 Feb 2014 in reply to Catalin Link to this post

    Hi Catalin,

    From Q1 2013 Telerik introduces a property called EnablePermissionsCheck, by default its value is true if you don't want to check the permission set it as false. 

    Thanks,
    Shinu.
  11. axxo
    axxo  avatar
    14 posts
    Member since:
    Feb 2014

    Posted 25 Feb 2014 Link to this post

    im also looking for the same thing
    i want to upload files to database directly instead of temp folder. the reason is that i found that file uploaded and then to move to database takes long time.

    im currently trying for entity framework. hwo to accomplish this and then how ot retrieve it? any idea?
  12. Peter Filipov
    Admin
    Peter Filipov avatar
    1028 posts

    Posted 28 Feb 2014 Link to this post

    Hi Axxo,

    You need to create a custom handler and  set the EnablePermissionsCheck = false and DisableChunkUpload = true. When the Process method in the Handler is overridden you should not call the base method (it saves the file into the temporary folder).

    Regards,
    Peter Filipov
    Telerik
    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 UI for ASP.NET AJAX, subscribe to the blog feed now.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017