简体   繁体   中英

uploading photo without any button into database and shown it instantly with AJAX telerik AsyncUpload

hi please see this Demo .
i use telerik in my project and i need an uploader that no need any button to upload and show image.So i write all demo code but this never work.i think my table in db is problem because image save in temp folder but no save in database and showing.please see this image and help how i fix this.
my table:

Column Name     Type            Nullable    Identity
-----------     --------        ---------   ---------
ImageID         int             No          Yes
ImageData       varbinary(MAX)  Yes         No
ImageName       nvarchar(200)   Yes         No
UserID          int             Yes         No


Thanks.

I think the problem in your code when you are inserting the image into your database so try this

System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand("insert  into table(ImageData,ImageName) values (@data,@name)");
command.Parameters.AddWithValue("@data", Uploader.fileBytes);
command.Parameters.AddWithValue("@name", "filename");
command.ExecuteNonQuery()

You can check the following code I tried to select an image from the File Browse dialog and upload it directly into my database.

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

Handler.ashx:

<%@ WebHandler Language="C#" Class="CustomHandler" %>
using System;
using System.Web;
using Telerik.Web.UI;
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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM