开发者

Silverlight 3 Progress Bar when uploading a file

I'm new to Silverlight and I have created a silverlight application to resize and upload images. The problem is the scroll bar doesn't update until the file has finished uploading. I have included sample code of the PushData function and the event handler. Please someone tell me what I'm doing wrong this problem is driving me crazy.

void btnTotalSizes_Click(object sender, RoutedEventArgs e)
{
    // ...
    UriBuilder ub = new UriBuilder("http://localhost:21636/FileReceiver.ashx");
    WebClient wc = new WebClient();
    wc.OpenWriteCompleted += (s2, e2) =>
    {
        PushData(outStream, e2.Result);
        e2.Result.Close();
        outStream.Close();
    };
    wc.OpenWriteAsync(ub.Uri);
}

private static void PushData(Stream input, Stream output)
{
    byte[] buffer = new byte[4096];
    int bytesRead = 0;
    int tempTotal = 0;

    whi开发者_开发技巧le ((bytesRead = input.Read(buffer, 0, buffer.Length)) != 0)
    {
        output.Write(buffer, 0, bytesRead);
        tempTotal += bytesRead;
        _uploadProg++;
        int percentDone = (int)(
            ((decimal)tempTotal / (decimal)input.Length) * 100);
        ProgressUpdated(null, new UploadProgressChangedEventArgs(percentDone));
    } 
}

void MainPage_ProgressUpdated(object sender, UploadProgressChangedEventArgs e)
{
    progFileProg.Maximum = 100;
    progFileProg.Value = e.ProgressPercentage;
    sbUpdateProg.Begin();
} 


You need to handle the WebClient.UploadProgressChanged event.

The event handler should update periodically. In the handler, update your UI.


I managed to get it going now. I did to use Dispatcher.BeginInvoke() as was commented by Rubens Farias. Also I had to add the uploading loop into a new thread. I assumed before the async callback would be In a new thread but I guess not.

public static event EventHandler UploadCompleted;
        public static event ProgressChangedEvent ProgressUpdated;

        public static int _fileLength;
        public static int _uploadProg;


        private List<FileInfo> _allSelectedFiles = new List<FileInfo>();


        public MainPage()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(MainPage_Loaded);

            btnAnimate.Click += new RoutedEventHandler(btnAnimate_Click);
            btnTotalSizes.Click += new RoutedEventHandler(btnTotalSizes_Click);
            ProgressUpdated += new ProgressChangedEvent(MainPage_ProgressUpdated);
        }

        void MainPage_ProgressUpdated(object sender, UploadProgressChangedEventArgs e)
        {
            Dispatcher.BeginInvoke(() =>
            {
                progFileProg.Maximum = 100;
                progFileProg.Value = _uploadProg;
                sbUpdateProg.Begin();
            });
        }


        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
        }

        void btnTotalSizes_Click(object sender, RoutedEventArgs e)
        {
            foreach (FileInfo fi in _allSelectedFiles)
            {
                Stream fileStream = fi.OpenRead();


                //DecodedJpeg jpegIn = new JpegDecoder(fileStream).Decode();
                //DecodedJpeg jpegOut = new DecodedJpeg(
                //    new ImageResizer(jpegIn.Image)
                //        .Resize(1500, ResamplingFilters.NearestNeighbor),
                //    jpegIn.MetaHeaders); // Retain EXIF details
                //MemoryStream outStream = new MemoryStream();
                //new JpegEncoder(jpegOut, 90, outStream).Encode();

                //outStream.Seek(0, SeekOrigin.Begin);
                //outStream.Position = 0;
                //UploadFile(fi.Name, outStream, "resources");

                UriBuilder ub = new UriBuilder("http://localhost:21636/FileReceiver.ashx");
                WebClient wc = new WebClient();

                //wc.UploadProgressChanged += (s2, e2) =>
                //{
                //    ProgressUpdated(null, new UploadProgressChangedEventArgs(_uploadProg));
                //};



                wc.OpenWriteCompleted += (s2, e2) =>
                {
                    System.Threading.Thread thr = new Thread(delegate()
                    {
                        PushData(fileStream, e2.Result);
                        e2.Result.Close();
                        fileStream.Close();
                    });
                    thr.Start();
                };
                wc.OpenWriteAsync(ub.Uri);

            }
        }





        private static void PushData(Stream input, Stream output)
        {
            byte[] buffer = new byte[4096];
            int bytesRead = 0;
            int tempTotal = 0;



            while ((bytesRead = input.Read(buffer, 0, buffer.Length)) != 0)
            {
                output.Write(buffer, 0, bytesRead);
                tempTotal += bytesRead;
                _uploadProg++;
                int percentDone = (int)(((decimal)tempTotal / (decimal)input.Length) * 100);
                _uploadProg = percentDone;
                ProgressUpdated(null, new UploadProgressChangedEventArgs(percentDone));

                Thread.Sleep(50);
            } 
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜