How to consume a file with a ServiceStack client

后端 未结 4 658
盖世英雄少女心
盖世英雄少女心 2021-02-01 23:34

I am trying to use ServiceStack to return a file to a ServiceStack client in a RESTful manner.

I have read other questions on SO (here and here) which advise using HttpR

4条回答
  •  执笔经年
    2021-02-02 00:14

    You wouldn't consume files with the ServiceStack's .NET ServiceClients as they're mainly for sending DTO's.

    You can just use any normal WebRequest to download files, in the v3.9.33 of ServiceStack introduced some handy WebRequest extensions HTTP Utils that make this easy, e.g:

    For a text file:

    var xmlFile = downloadUrl.GetXmlFromUrl(responseFilter: httpRes => {
            var fileInfoHeaders = httpRes.Headers[HttpHeaders.ContentDisposition];
        });
    

    Where fileInfoHeaders contains the W3C ContentDisposition HTTP Header, e.g. when returning a FileInfo, ServiceStack returns:

    attachment;filename="file.xml";size={bytesLen};
    creation-date={date};modification-date={date};read-date={date};
    

    To download a binary file you can use:

    var rawBytes = downloadUrl.GetBytesFromUrl(httpRes => ...);
    

提交回复
热议问题