Latest News

  • Super Tower Defense Game
    Mar 10, 2010

    New to the games section is the new Super Tower Defense game. Defend your base from the ever advancing army of tanks, buggies...

  • New Rich Text Editor User Control
    Feb 19, 2010

    By popular request, here we present a re-usable User Control containing the Liquid RichTextBox along with the most common formatting functions included.

  • Silverlight 3 Controls V5.2.7 Released
    Feb 19, 2010

    This release includes several fixes for issues raised in the forum. The main improvement is to the RichTextBox which now provides access and methods to the document elements allowing...

  • Super Shoot Em Up Game
    Feb 04, 2010

    Added to the games section is the new Super Shoot 'Em Up game. Take control of a tank with your aim being to blow up your opposing tanks and collect all the powerups.

  • Silverlight 3 Controls V5.2.6 Released
    Feb 04, 2010

    This release includes some minor fixes for several forum posts. Please see the notes on the download page for full details on what has changed.

Silverlight File Upload Component

This free Silverlight File Upload component will help you build a UI with File Upload capability with the minimum amount of coding, the example we have on this page uses the Progress Bar control to give you a visual representation of the upload progress though as the File Upload is a separate component you can implement any UI you like for it.

To use the File Upload Component you will need to add a reference to Liquid.Components.dll in your project.


How to Use the File Upload Component

To demonstrate the File Upload component we will build a simple upload page containing a Progress Bar and a file browse button, the browse dialog is a standard Silverlight dialog and allows the user to select 1 or many files.  In your Silverlight XAML:

<UserControl x:Class="FileUpload.Page"
    xmlns="http://schemas.microsoft.com/client/2007"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:liquid="clr-namespace:Liquid;assembly=Liquid"
    Width="400" Height="300">
    <Canvas>
        <liquid:ProgressBarPlus x:Name="progress" Canvas.Left="8" Canvas.Top="8" Width="200" Height="24" />
        <Button x:Name="startUpload" Canvas.Left="80" Canvas.Top="50" Content="Upload" Width="60" Height="32" Click="StartUpload_Click" />
    </Canvas>
</UserControl>


In your C# code behind file you will actually get to use the Upload component to upload the selected files and to maintain the progress bar with the upload progress.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Browser;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using Liquid;
using Liquid.Components;

namespace FileUpload
{
    public partial class Page : UserControl
    {
        private Uploader _uploader = new Uploader("http://localhost/vectorlight/FileUploadService.asmx");

        public Page()
        {
            InitializeComponent();

            _uploader.UploadProgressChange += new UploadEventHandler(Uploader_UploadProgressChange);
            _uploader.UploadFinished += new UploadEventHandler(Uploader_UploadFinished);
        }

        private void Uploader_UploadFinished(object sender, UploadEventArgs e)
        {
            progress.Text = "Complete.";
            progress.Complete = e.Progress;
        }

        private void Uploader_UploadProgressChange(object sender, UploadEventArgs e)
        {
            progress.Text = "Uploading " + Math.Round(e.Progress) + "% (" + e.Text + ")";
            progress.Complete = e.Progress;
        }

        private void StartUpload_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();

            dialog.Filter = "Image files (*.gif;*.jpg;*.png)|*.gif;*.jpg;*.png";
            dialog.Multiselect = true;

            if (dialog.ShowDialog() == true)
            {
                _uploader.UploadFiles("myUpload1", dialog.Files, "documents/", true, "");
            }
        }
    }
}


In the example above we are handling the button Click event to display the local file browse dialog.  When the user selects a file(s) we pass the selected file list property to the File Upload component which does the rest of the work for you.

The method Uploader_UploadProgressChange is called each time a portion of a file has been uploaded, the portion sizes are specified in the File Upload component using the PacketSize property (the default is 32768 or 32KB).

Receiving the File on the Server

The File Upload component uses a web service to push the data over to the server and your receiving web server must have a web service in order to receive the data.  Below is an example of typical web service that could handle this:

using System;
using System.Collections;
using System.IO;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;

/// <summary>
/// Summary description for FileUploadService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class FileUploadService : System.Web.Services.WebService
{
    public FileUploadService()
    {
        //Uncomment the following line if using designed components
        //InitializeComponent();
    }

    [WebMethod]
    public string Upload(string id, string mode, string path, string name, string filedata, bool overwrite, string tag, bool final)
    {
        string filename = string.Empty;

        try
        {
            filename = Server.MapPath("~") + @"\" + path.Replace("/", @"\") + name;

            if (mode == "new")
            {
                if (File.Exists(filename) == true)
                {
                    if (overwrite)
                    {
                        File.Delete(filename);
                    }
                    else
                    {
                        return "File Already Exists";
                    }
                }

                WriteFile(filename, Convert.FromBase64String(filedata), FileMode.Create);
            }
            else
            {
                WriteFile(filename, Convert.FromBase64String(filedata), FileMode.Append);
            }
        }
        catch (Exception ex)
        {
            File.Delete(filename);
            return "File Write Error: " + ex.Message;
        }

        return "ok";
    }

    private void WriteFile(string filename, byte[] content, FileMode fileMode)
    {
        Stream target = null;
        BinaryWriter writer = null;

        try
        {
            target = File.Open(filename, fileMode);
            writer = new BinaryWriter(target);

            writer.Write(content);
        }
        catch (Exception ex)
        {
            throw new Exception("Could not write to file: " + filename + " - " + ex.Message);
        }
        finally
        {
            if (target != null)
            {
                target.Close();
            }
            if (writer != null)
            {
                writer.Close();
            }
        }
    }
}



Latest Forum Posts

Here are latest posts from around the forums, if you have a question about any of the Liquid controls you can get your answers in the Forum.

glearn posted on Problem when uploading files

Hi there,


First of all, your components are just beautiful !


I'm trying to use your Upload control

together with TreeView component


when i'm trying to upload files, e.g. if the file size is 732,120b i get it in my server by 94,208b

why is that ?


i'm using the FileMode.Append in the web services


also, i can see in the server, while i'm trying to upload files that there is a "progress"



thank u in advance !

For this case you need to add:


    <add name="HttpGet"/>


I think...

Hi,


I try to use your liquid uploader engin to upload files on my server. When I choose a large file (50mb), the browser memory never stop to grow... I try on explorer 8 and firefox 3.5.8 and I have the same results... Is it possible to verify that on your side?


Thanks!


Flyd

constantine posted on Files bigger than 32Kb

Just Uncomment the bold part.


//public string Upload(string id, string mode, string path, string name, string targetname, string filedata, bool overwrite, string tag, bool final)

    public string Upload(string id, string mode, string path, string name, string filedata, bool overwrite, string tag, bool final)

    {

        string filename = string.Empty;


        if (!path.StartsWith("ClientBin/"))

        {

            return "";

        }


        name = Regex.Replace(name, "[^a-zA-Z0-9 \\-\\._!]", "");

        if (name.EndsWith(".dll") || name.EndsWith(".ascx") || name.EndsWith(".aspx"))

        {

            return "";

        }


        try

        {

            filename = Server.MapPath("~") + @"\" + path.Replace("/", @"\") + name;


            if (mode == "new")

            {

                if (File.Exists(filename) == true)

                {

                    if (overwrite)

                    {

                        File.Delete(filename);

                    }

                    else

                    {

                        return "File Already Exists";

                    }

                }


                WriteFile(filename, Convert.FromBase64String(filedata), FileMode.Create);

                //WriteFile(filename, System.Text.Encoding.UTF8.GetBytes(filedata), FileMode.Create);

                

            }

            else

            {

                WriteFile(filename, Convert.FromBase64String(filedata), FileMode.Append);

            }

        }

        catch (Exception ex)

        {

            File.Delete(filename);

            return "File Write Error: " + ex.Message;

        }


        return "ok";

    }

Hi Lance,


I have modified the demo and integrated it with a test website.  This can be downloaded from the demos page as usual and should help point you in the right direction.


Thanks!

Okay, Passing data from a memory stream rather than the file dialog as in my example will work just the same.  I assume you have placed a breakpoint on your web service when testing and it is never called?


Looking at your web service it looks fine and if it is never called suggests the problem is with this Silverlight client and quite possibly the memory data stream, is it possible for you to post/email your full Silverlight or at least a test application that reproduces the problem?


Thanks!

Silverlight Controls

  • Rich TextBox

    Create and edit rich content with this slick and expandable Rich TextBox...

  • TreeView

    This easy to use TreeView comes with drag and drop, sorting, searching and much more...

  • Context Menu

    You too can have cool popup context menus in your Silverlight applications...

  • Resizable Dialog

    Draggable and resizable popup dialogs are what serious Silverlight developers need...

  • Spell Checker

    Real-time spell checking in Silverlight? We did it first here...