在asp.net中等待()显示上传的图像

时间:2012-05-27 08:22:46

标签: asp.net silverlight image wait

我想强迫我的程序在做某事后等待一段时间,然后在asp.net和silverlight中做其他事情。 (详细地说,我想通过silverlight程序上传图像,然后通过Image控件在我的页面中显示它。但是当我上传大小约为6KB或更高的图像时,图像不会显示,但它有已成功上传。我认为等待一段时间可以解决问题) 愿任何人指导我吗? 谢谢

1 个答案:

答案 0 :(得分:0)

我使用this page

中的代码

这是MyPage.Xaml中的代码:

 private void UploadBtn_Click(object sender, RoutedEventArgs e)
    {

        OpenFileDialog dlg = new OpenFileDialog();

        dlg.Multiselect = false;
        dlg.Filter = UPLOAD_DIALOG_FILTER;
        if ((bool)dlg.ShowDialog())
        {
            progressBar.Visibility = Visibility.Visible;
            progressBar.Value = 0;
            _fileName = dlg.File.Name;
            UploadFile(_fileName, dlg.File.OpenRead());

        }

        else
        {

            //user clicked cancel

        }

    }


    private void UploadFile(string fileName, Stream data)
    {
        // Just kept here
        progressBar.Maximum = data.Length;

        UriBuilder ub = new UriBuilder("http://mysite.com/receiver.ashx");

        ub.Query = string.Format("fileName={0}", fileName);
        WebClient c = new WebClient();

        c.OpenWriteCompleted += (sender, e) =>
        {

            PushData(data, e.Result);

            e.Result.Close();

            data.Close();

        };

        try
        {
            c.OpenWriteAsync(ub.Uri);
        }
        catch (Exception e)
        {
            MessageBox.Show(e.ToString());
        }

    }
    private void PushData(Stream input, Stream output)
    {
        byte[] buffer = new byte[input.Length];

        int bytesRead;
        while ((bytesRead = input.Read(buffer, 0, buffer.Length)) != 0)
        {
            output.Write(buffer, 0, buffer.Length);
        }
        progressBar.Value += input.Length;

        if (progressBar.Value >= progressBar.Maximum)
        {
            progressBar.Visibility = System.Windows.Visibility.Collapsed;
            loadImage();
        }
    }
private void loadImage()
    {
        Uri ur = new Uri("http://mysite.com/upload/" + _fileName);
        img1.Source = new BitmapImage(ur);
    }

这是receiver.ashx:

public void ProcessRequest(HttpContext context)
{
    string filename = context.Request.QueryString["filename"].ToString(); using (FileStream fs = File.Create(context.Server.MapPath("~/upload/" + filename)))
    {

        SaveFile(context.Request.InputStream, fs);

    }

}
private void SaveFile(Stream stream, FileStream fs)
{

    byte[] buffer = new byte[stream.Length];

    int bytesRead;
    while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) != 0)
    {

        fs.Write(buffer, 0, bytesRead);

    }

}