Search code examples
c#sql-serversql-server-2008-r2asp.net-ajaxajaxcontroltoolkit

How to stream image file into a SQL DataSource with AjaxFileUpload?


I would like to upload multiple files into a SQL database using AjaxFileUpload. I have a method that I use to upload a single file; in the aspx page with:

<asp:FileUpload ID="file_Image" runat="server"/>

And in the aspx.cs page with:

protected void UploadFile(object sender, EventArgs e)
{
    FileUpload FileUpload1 = file_Image;

    // Read the file and convert it to Byte Array
    string filePath = file_Image.PostedFile.FileName;
    string filename = Path.GetFileName(filePath);

    if (FileUpload1.HasFile && FileUpload1.PostedFile != null)
    {
        Stream fs = file_Image.PostedFile.InputStream;
        BinaryReader br = new BinaryReader(fs);
        Byte[] bytes = br.ReadBytes((Int32)fs.Length);}}

However, how can I use something similar for AjaxFileUpload or is it even possible to stream image data like this from the Ajax control? Thanks a million for sharing your knowledge!


Solution

  • [Refer New AjaxFileUpload control in AjaxToolKit][1] http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/AjaxFileUpload/AjaxFileUpload.aspx

    This new AjaxFileUpload control supports multiple file upload at once. but it comes with some limitation that IE10 or Chrome latest version support this.

    This is very reliable, I am using this.

    Simple way to convert AjaxFileUpload file content into sql varbinary array:

    protected void AjaxFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
    {
    byte[] image = e.GetContents();
    }