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

Upload to DB without temporary folder upload.

10 Answers 109 Views
AsyncUpload
This is a migrated thread and some comments may be shown as answers.
Dona
Top achievements
Rank 1
Dona asked on 05 Aug 2013, 02:33 AM
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

10 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 1
answered on 05 Aug 2013, 04:36 AM
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.
0
Harald
Top achievements
Rank 1
answered on 21 Oct 2013, 06:06 AM

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

0
Shinu
Top achievements
Rank 1
answered on 22 Oct 2013, 11:04 AM
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.
0
Harald
Top achievements
Rank 1
answered on 22 Oct 2013, 08:14 PM

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

0
JC
Top achievements
Rank 1
answered on 05 Nov 2013, 05:25 PM
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?
0
Shinu
Top achievements
Rank 1
answered on 06 Nov 2013, 05:16 AM
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.
0
Catalin
Top achievements
Rank 1
answered on 14 Feb 2014, 04:07 PM
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
0
Shinu
Top achievements
Rank 1
answered on 17 Feb 2014, 10:48 AM
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.
0
axxo
Top achievements
Rank 1
answered on 25 Feb 2014, 09:02 PM
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?
0
Peter Filipov
Telerik team
answered on 28 Feb 2014, 12:52 PM
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.
Tags
AsyncUpload
Asked by
Dona
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 1
Harald
Top achievements
Rank 1
JC
Top achievements
Rank 1
Catalin
Top achievements
Rank 1
axxo
Top achievements
Rank 1
Peter Filipov
Telerik team
Share this question
or