A XAML browser application (XBAP) running in partial trust can safely upload files from a client machine. : Page « Windows Presentation Foundation « C# / CSharp Tutorial






<Page x:Class="SafeFileUploadPartialTrustSample.HomePage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="HomePage"
  xmlns:dialogs="clr-namespace:Microsoft.Win32;assembly=PresentationFramework">
    <StackPanel>
    <Button Name="uploadButton" Click="uploadButton_Click" Width="100">Upload Image...</Button>
    <Image Name="viewImage" Width="500" Height="300"></Image>
    <Label Name="nameLabel"></Label>
    
    </StackPanel>
</Page>
//File:Window.xaml.cs
using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using Microsoft.Win32;

namespace SafeFileUploadPartialTrustSample {
    public partial class HomePage : Page, IProvideCustomContentState {
        public HomePage() {
            InitializeComponent();
        }

        void uploadButton_Click(object sender, RoutedEventArgs e) {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.*";

            if (dlg.ShowDialog() == true) {
                if (this.viewImage.Source != null) {
                    ImageCustomContentState iccs = new ImageCustomContentState(this.viewImage.Source, (string)this.nameLabel.Content);
                    this.NavigationService.AddBackEntry(iccs);
                }
                using (Stream stream = dlg.OpenFile()) {
                    BitmapDecoder bitmapDecoder = BitmapDecoder.Create(stream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
                    this.viewImage.Source = bitmapDecoder.Frames[0];
                    this.nameLabel.Content = dlg.SafeFileName;
                }
            }
        }
        public CustomContentState GetContentState() {
            return new ImageCustomContentState(this.viewImage.Source, (string)this.nameLabel.Content);
        }
    }
    [Serializable]
    public class ImageCustomContentState : CustomContentState
    {

        ImageSource imageSource;
        string filename;

        public ImageCustomContentState(ImageSource imageSource, string filename)
        {
            this.imageSource = imageSource;
            this.filename = filename;
        }

        public override string JournalEntryName
        {
            get { return this.filename; }
        }

        public override void Replay(NavigationService navigationService, NavigationMode mode)
        {
            HomePage homePage = (HomePage)navigationService.Content;
            homePage.viewImage.Source = this.imageSource;
            homePage.nameLabel.Content = this.filename;
        }
    }
}
WPF A X A M L Browser Application X B A P Running In Partial Trust Can Safely Upload Files From A Client Machine








24.69.Page
24.69.1.Page Loaded eventPage Loaded event
24.69.2.Remember and navigate through multiple sets of state for a single page instanceRemember and navigate through multiple sets of state for a single page instance
24.69.3.Navigate to an instance of a custom class, instead of a Page.Navigate to an instance of a custom class, instead of a Page.
24.69.4.Create a button when the page loads.Create a button when the page loads.
24.69.5.UI With Page for Dynamic Button contentUI With Page for Dynamic Button content
24.69.6.A XAML browser application (XBAP) running in partial trust can safely upload files from a client machine.A XAML browser application (XBAP) running in partial trust can safely upload files from a client machine.