Uploader for Silverlight
Uploading and Previewing Files

You can use Uploader for Silverlight in combination with PDF for Silverlight to create and upload PDF files to the server. In this topic we use the C1Uploader class along with C1Pdf to create PDF files uploaded to a stream so a user can preview files to print. Note that for simplicity the project uses the C1UploaderHelper.cs code file included with the Uploader for Silverlight samples.

Complete the following steps to create the project:

  1. In Visual Studio 2008, select File | New | Project.
  2. In the New Project dialog box, select a language in the left pane, and in the templates list select Silverlight Application. Enter a Name for your project and click OK. The New Silverlight Application dialog box appears.
  3. Click OK to accept default settings, close the New Silverlight Application dialog box, and create your project. The MainPage.xaml file opens.
  4. Right-click the project in the Solution Explorer window and select Add Reference.
  5. In the Add Reference dialog box, locate and select the C1.Silverlight.Pdf.dll and C1.Silverlight.Uploader.dll assemblies and click OK to add references to your project.
  6. in the MainPage.xaml file, between the <Grid> and </Grid> tags, add the following XAML markup:
    XAML
    Copy Code
    <StackPanel Width="300" HorizontalAlignment="Center" VerticalAlignment="Center"> 
        <TextBlock Text="Enter text here:" Margin="10"/> 
        <TextBox Name="tb" Margin="10" Height="100" TextWrapping="Wrap"/> 
        <Button Content="Print and Open PDF" Margin="10" Click="Button_Click" /> 
    </StackPanel> 
    
    This markup creates a text box to enter text to include in the PDF and a button for users to click to turn the text into a PDF and upload the file to the server.
  7. In the Solution Explorer, right-click the MyProjectWeb application where "MyProject" is the name of your project application. Select New Folder and name the folder "temp." This folder contains the PDF file.

Complete the following steps to code the button click and upload methods:

  1. In the Solution Explorer, right-click MainPage.xaml file and select View Code to switch to Code view.
  2. In Code view, add the following import statements to the top of the page if they are not included:
    Visual Basic
    Copy Code
    Imports System.IO 
    Imports C1.Silverlight.Pdf 
    Imports C1.Silverlight.Uploader 
    

     

    C#
    Copy Code
    using System.IO; 
    using C1.Silverlight.Pdf; 
    using C1.Silverlight.Uploader; 
    
  3. Add the following code to the MainPage class:
    Visual Basic
    Copy Code
    Private Sub Button_Click(ByVal sender As Object, ByVal e As RoutedEventArgs) 
        ' Create PDF 
        Dim doc As New C1PdfDocument() 
        Dim st As String = Nothing 
    
        st = tb.Text 
    
        doc.DrawString(st, New Font("Arial", 14), Colors.Black, New Rect(0, 0, 500, 100)) 
    
        ' Generate stream 
        Dim stream As Stream 
        stream = New MemoryStream() 
        doc.Save(stream) 
    
        ' Upload stream 
        Dim uploader = CreateUploader() 
        uploader.AddFile("test.pdf", stream) 
        uploader.BeginUploadFiles() 
        AddHandler uploader.UploadCompleted, AddressOf uploader_UploadCompleted 
    End Sub 
    
    Private Sub uploader_UploadCompleted(ByVal sender As Object, ByVal e As UploadCompletedEventArgs) 
        ' Navigate to PDF 
        If e.[Error] Is Nothing Then 
            Dim url = New Uri(DirectCast(e.Response, String())(0)) 
            System.Windows.Browser.HtmlPage.Window.Navigate(url) 
        End If 
    End Sub 
    
    Private Function CreateUploader() As C1Uploader 
        Dim mpUploader As New C1UploaderPost(FilesPerRequest.AllFilesInOneRequest) 
        mpUploader.Settings.Address = C1.Silverlight.Extensions.GetAbsoluteUri("Handler.ashx") 
        Return mpUploader 
    End Function 
    

     

    C#
    Copy Code
    private void Button_Click(object sender, RoutedEventArgs e) 
    { 
        // Create PDF 
        C1PdfDocument doc = new C1PdfDocument(); 
        doc.DrawString(tb.Text, new Font("Arial", 12), Colors.Black, new Rect(0, 0, 500, 100)); 
    
        // Generate stream 
        Stream stream; 
        stream = new MemoryStream(); 
        doc.Save(stream); 
    
        // Upload stream 
        var uploader = CreateUploader(); 
        uploader.AddFile("test.pdf", stream); 
        uploader.BeginUploadFiles(); 
        uploader.UploadCompleted += new EventHandler<UploadCompletedEventArgs>(uploader_UploadCompleted); 
    } 
    
    void uploader_UploadCompleted(object sender, UploadCompletedEventArgs e) 
    { 
        // Navigate to PDF 
        if (e.Error == null) 
        { 
            var url = new Uri(((string[])e.Response)[0]); 
            System.Windows.Browser.HtmlPage.Window.Navigate(url); 
        } 
    } 
    
    private C1Uploader CreateUploader() 
    { 
        C1UploaderPost mpUploader = new C1UploaderPost(FilesPerRequest.AllFilesInOneRequest); 
        mpUploader.Settings.Address = C1.Silverlight.Extensions.GetAbsoluteUri("Handler.ashx"); 
        return mpUploader; 
    } 
    

Complete the following steps to process requests:

  1. In the Visual Studio Solution Explorer, right-click the MyProjectWeb solution where "MyProject" is the name of your project. Select the Add | Existing Item option.
  2. In the Add Existing Item dialog box, navigate to the ControlExplorerWeb folder where samples are installed, select the C1UploaderHelper.cs file, and click the Add button.
    By default the ControlExplorerWeb folder is located in the Documents or My Documents folder at ComponentOne Samples\Studio for Silverlight\ControlExplorer\ControlExplorerWeb.
  3. In the Visual Studio Solution Explorer, right-click the MyProjectWeb solution where "MyProject" is the name of your project. Select the Add | New Item option.
  4. In the Add New Item dialog box, select the Generic Handler template and name the new class "Handler.ashx."
  5. Click Add to add the file to your project and close the Add New Item dialog box.
  6. Open the Handler.ashx code file and add the following import statement (where "MyProject" is the name of your project application in the Solution Explorer) to the top of the page if it is not included:
    Visual Basic
    Copy Code
    Imports System.Web 
    Imports System.Web.Services 
    Imports MyProject 
    

     

    C#
    Copy Code
    using System.Web; 
    using System.Web.Services; 
    using MyProject; 
    
  7. Replace the default implementation of the ProcessRequest method with the following code:
    Visual Basic
    Copy Code
    Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest 
        Try 
            ' Get custom parameters 
            Dim parameters As String = context.Request.Params("parameters") 
    
            For i As Integer = 0 To context.Request.Files.Count - 1 
            ' Get the uploaded file, and calculates the full path to save it in the server 
            Dim file As HttpPostedFile = context.Request.Files(i) 
            Dim serverFileName As String = C1UploaderHelper.GetServerPath(context.Server, file.FileName) 
    
            If C1UploaderHelper.ProcessPart(context, file.InputStream, serverFileName, 1, 1) Then 
                ' Add the URL of the uploaded file to the response 
                Dim url As String = C1UploaderHelper.GetUploadedFileUrl(context, "Handler.ashx",file.FileName) 
                context.Response.Write((If(i > 0, "|", String.Empty)) + url) 
            Else 
                C1UploaderHelper.WriteError(context, C1UploaderHelper.ERROR_MESSAGE) 
                Exit For 
            End If 
            Next 
            Catch exc As Exception 
            C1UploaderHelper.WriteError(context, exc.Message) 
            context.Response.[End]() 
        End Try 
    End Sub 
    

     

    C#
    Copy Code
    public void ProcessRequest(HttpContext context) 
    { 
        try 
        { 
            // Get custom parameters 
            string parameters = context.Request.Params["parameters"]; 
            for (int i = 0; i < context.Request.Files.Count; i++) 
            { 
                // Get the uploaded file, and calculates the full path to save it in the server 
                HttpPostedFile file = context.Request.Files[i]; 
                string serverFileName = C1UploaderHelper.GetServerPath(context.Server,file.FileName); 
                if (File.Exists(file.FileName)) 
                    File.Delete(file.FileName); 
                if (C1UploaderHelper.ProcessPart(context, file.InputStream, serverFileName, 1, 1)) 
                { 
                    // Add the URL of the uploaded file to the response 
                    string url = C1UploaderHelper.GetUploadedFileUrl(context,"Handler.ashx", file.FileName); 
                    context.Response.Write((i > 0 ? "|" : string.Empty) + url); 
                } 
                else 
                { 
                    C1UploaderHelper.WriteError(context, C1UploaderHelper.ERROR_MESSAGE); 
                    break; 
                } 
            } 
        } 
        catch (Exception exc) 
        { 
            C1UploaderHelper.WriteError(context, exc.Message); 
            context.Response.End(); 
        } 
    } 
    

What You've Accomplished

You've successfully created a Silverlight application that uploads and previews text in a PDF file. To observe how the application works, complete the following steps:

  1. To run the application, select Debug | Start Debugging.
  2. Enter text in the text box, for example "Hello World!"
  3. Click the Print and Open PDF button.

The text you entered is added to the test.pdf file and opened. You can close the PDF file, enter new text in the text box, and click the button. The new text appears in the PDF file that opens.

See Also

 

 


Copyright (c) GrapeCity, inc. All rights reserved.

Product Support Forum  |  Documentation Feedback