Book Image

ServiceStack 4 Cookbook

Book Image

ServiceStack 4 Cookbook

Overview of this book

Table of Contents (18 chapters)
ServiceStack 4 Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Handling file uploads and streaming data from services


A common requirement for web applications is to enable users to upload files to a server. In this recipe, we will build a service to handle file uploads and give an example of it working with both a web client and a .NET client.

Getting ready

If you are starting with a new project, please go to section Creating a ServiceStack solution with Visual Studio and NuGet, Appendix A, Getting Started, to help you get set up.

How to do it…

  1. Create the request object for the file upload service, as follows:

    [Route("/fileupload/simple")]
    public class SimpleFileUploadRequest
    {
      public string FileName { get; set; }
      public string UploadedByUserName { get; set; }
    } 
  2. Create the service to handle the file upload, as follows:

    public object Post(SimpleFileUploadRequest request)
    {
      if (Request.Files.Length == 0)
      {
        throw new HttpError(HttpStatusCode.BadRequest, "NoFile");
      }
      string resultFile = @"../../" + Request.Files[0].FileName;
      Request.Files[0...