Uploader for Silverlight Task-Based Help > 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:
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> |
Complete the following steps to code the button click and upload methods:
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; |
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:
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; |
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(); } } |
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:
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.