1

I am trying upload multiple file using service stack. Below code is working fine for one file upload. I want to upload multiple file. Please let me know what change should be required so that below codes work for multiple files upload also.

 public class Hello : IRequiresRequestStream
    {
        Stream RequestStream { get; set; }
    }

At client side I am using 'multipart/form-data' for file upload.

chirag
  • 1,818
  • 1
  • 15
  • 36

1 Answers1

2

See the documentation on Uploading Files, IRequiresRequestStream is only for accessing the Request Body as a Stream of Bytes, to process multiple files uploaded with multipart/form-data use the base.Request.Files property instead, e.g:

Uploading Files

You can access uploaded files independently of the Request DTO using Request.Files. e.g:

public object Post(MyFileUpload request)
{
    if (this.Request.Files.Length > 0)
    {
        var uploadedFile = base.Request.Files[0];
        uploadedFile.SaveTo(MyUploadsDirPath.CombineWith(file.FileName));
    }
    return HttpResult.Redirect("/");
}

ServiceStack's imgur.servicestack.net example shows how to access the byte stream of multiple uploaded files, e.g:

public object Post(Upload request)
{
    foreach (var uploadedFile in base.Request.Files
       .Where(uploadedFile => uploadedFile.ContentLength > 0))
    {
        using (var ms = new MemoryStream())
        {
            uploadedFile.WriteTo(ms);
            WriteImage(ms);
        }
    }
    return HttpResult.Redirect("/");
}
mythz
  • 141,670
  • 29
  • 246
  • 390
  • I am not able to access object this.Request.Files. it is showing error like "The Namespace or module 'Request' is not define ". – chirag Aug 17 '15 at 06:44
  • @chirag base.Request is a normal property on the 'Service' base class, it will be there if your service inherits from Service. – mythz Aug 17 '15 at 07:16
  • In servicestack we did all communication using RequestDTO. But here we directly access file using base.Request.Files . Suppose I access file using base.Request.Files then Is there any change of violation of message base design pattern? Beacause we are not using RequestDTO class object of file reading. – chirag Aug 18 '15 at 08:57
  • @mythz- Is there any ways that we can access file using requestDTO object.? Means using RequestStream property of DTO class. – chirag Aug 18 '15 at 11:47
  • @chirag no files are read directly from the underlying ASP.NET request object – mythz Aug 18 '15 at 14:26
  • @mythz- base.Request object is behave similar as ASP.NET request object. am I right? we are reading file without using RequestDTO class. If we are reading file using RequestDTO class then we get file using RequestStream(Type of Stream). – chirag Aug 18 '15 at 15:21
  • @chirag base.Request is a thin wrapper over ASP.NET's Request, but you can't read `multipart/form-data` Request directly as a stream of bytes as mentioned in the first paragraph of this answer. – mythz Aug 18 '15 at 15:33