简体   繁体   English

使用 ServiceStack 下载文件(html 内容)

[英]Download file (html content) with ServiceStack

I'm using Service Stack for a simple web application.我将服务堆栈用于一个简单的 Web 应用程序。

In this app i need to "export" some content to Excel.. So this is the approach I took:在这个应用程序中,我需要将一些内容“导出”到 Excel。所以这是我采取的方法:

  1. I get the HTML content of a table with jQuery a get the HTML content of a table, and send to the service.我使用 jQuery 获取表格的 HTML 内容,并获取表格的 HTML 内容,然后发送到服务。
  2. The Service write the content to a file (with some CSS)服务将内容写入文件(带有一些 CSS)
  3. Using the same service, but with a GET method, I read the file and make the download with ContentType:"application/vnd.ms-excel"使用相同的服务,但使用GET方法,我读取文件并使用ContentType:"application/vnd.ms-excel"

I used this approach without Service Stack in other times, and worked well.. The XLS file had the right content and some colors.我在其他时候使用了这种没有 Service Stack 的方法,效果很好.. XLS文件有正确的内容和一些颜色。

Now, when I get the file using GET method (ie, visiting the url with the browser), the file contents are bad.现在,当我使用GET方法(即使用浏览器访问 url)获取文件时,文件内容是错误的。

ServiceStack let download with some formats: html, csv, jsv, json, xml. ServiceStack 允许下载一些格式:html、csv、jsv、json、xml。

The html format shows up a default report page, the only format that works a little is jsv. html 格式显示了一个默认的报告页面,唯一有效的格式是 jsv。

My question is: how can I download the file like plain html file?我的问题是:如何下载纯 html 文件之类的文件?

Some code:一些代码:

public class ExcelService : RestServiceBase<Excel>
{   
    public override object OnGet (Excel request)
    {
        string file = request.nombre;
        //Response.Clear();
        HttpResult res = new HttpResult();
        res.Headers[HttpHeaders.ContentType] = "application/vnd.ms-excel";
        res.Headers[HttpHeaders.ContentDisposition] = "attachment; filename="+file+".xls";
        string archivo = System.IO.File.ReadAllText("tmp/"+file+".html");
        res.Response = archivo;
        return res;
    }
}

Thanks in advance提前致谢

The HttpResult Constructor in ServiceStack as an overload that supports file downloads, eg: ServiceStack 中的 HttpResult 构造函数作为支持文件下载的重载,例如:

return new HttpResult(new FileInfo("tmp/"+file+".html"), 
    asAttachment: true, 
    contentType: "application/vnd.ms-excel");

Which will set the ContentDisposition HTTP headers required for file downloading.这将设置文件下载所需的ContentDisposition HTTP 标头。

Creating your own Http Result创建自己的 Http 结果

For more fine-grained control of the HttpOutput you can also create your own Result class.为了对 HttpOutput 进行更细粒度的控制,您还可以创建自己的 Result 类。 Here is an example of downloading an Excel spreadsheet taken from the customized responses in ServiceStack question :以下是从ServiceStack question 中自定义响应中下载 Excel 电子表格的示例:

public class ExcelFileResult : IHasOptions, IStreamWriterAsync
{
    private readonly Stream _responseStream;
    public IDictionary<string, string> Options { get; private set; }

    public ExcelFileResult(Stream responseStream)
    {
        _responseStream = responseStream;

        Options = new Dictionary<string, string> {
             {"Content-Type", "application/octet-stream"},
             {"Content-Disposition", ""attachment; filename=\"report.xls\";"}
         };
    }

    public async Task WriteToAsync(Stream responseStream, CancellationToken token = default)
    {
        if (_responseStream == null) 
            return;

        await _responseStream.CopyToAsync(responseStream, token);
        await responseStream.FlushAsync(token);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM