网页API返回的文本文件,除非垃圾从浏览器栏运行 [英] Web Api returns garbage for text files unless run from the browser bar

查看:97
本文介绍了网页API返回的文本文件,除非垃圾从浏览器栏运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Asp.Net的Web API编写的文件服务。该服务检索SQL Server文件(CSS时,Excel,CSV等)和响应GET请求提供他们。

I am writing a file service using Asp.Net’s Web Api. The service retrieves files (Css, Excel, Csv, etc.) from SQL Server and serves them up in response to Get requests.

我的第一个测试用例的CSS文件。问题是,虽然我可以看到在服务器端正确的数据,当浏览器检索/德codeS吧,结果是错位。这个问题似乎是相关的编码。

My first test case is for Css files. The issue is that, while I can see the correct data on the server side, when the browser retrieves/decodes it, the results are mangled. The issue appears to be related to the encodings.

下面是请求/响应头在FireFox:

Here are the request/response headers in FireFox:

当我点击萤火响应选项卡上,这里是什么样子:

When I click on the response tab in FireBug, here’s what it looks like:

结果看起来像ASCII显示为UTF8。这是在Firebug的HTML视图:

The results look like ascii being displayed as utf8. This is the html view in FireBug:

上面的例子是运行SSL一个Facebook应用程序中的iFrame。

The above example is an iFrame inside a Facebook application which is running ssl.

如果我把网址,直接在浏览器中打开它,它的工作原理和正确显示我的CSS:

If I take the url and open it directly in the browser, it works and correctly displays my Css:

总之,当我检索标签我的CSS文件,我的Facebook应用程序里面,我得到垃圾(编码问题?)。如果我从浏览器直接检索它,它的工作原理。

In summary, when I retrieve my Css file from a tag inside my Facebook app, I get garbage (encoding issue?). If I retrieve it straight from the browser, it works.

我CssFormatter MediaTypeFormatter code:

My CssFormatter MediaTypeFormatter code:

    public override Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content, TransportContext transportContext)
    {

        var taskSource = new TaskCompletionSource<object>(); 
        try
        {
            var incomingFile = value as FileRestService.Entity.IFile;

            var ms = new MemoryStream(incomingFile.DataBuffer);

            ms.CopyTo(writeStream);
            ms.Flush();

            taskSource.SetResult(writeStream);

        }
        catch (Exception e) 
        { 
            taskSource.SetException(e); 
        } 
        return taskSource.Task; 

    }

是我创造的响应流不正确?我注意到,响应头没有指定编码。这是一个问题?

Am I creating the response stream incorrectly? I noticed that the response headers do not specify the encoding. Is this an issue?

推荐答案

我觉得处理这个最简单的方法是写沿行(这里是重要的细节)的内容:

I find the easiest way to handle this is to write something along the lines of (here's the important details):

public class Formatter : MediaTypeFormatter {
    // TODO override the constructor to add some mappings or some other way for this formatter to be picked up

    // TODO override CanReadType and CanWriteType according to your rules

    public override void SetDefaultContentHeaders(Type t, HttpContentHeaders headers, string mediaType) {
        base.SetDefaultContentHeaders(t, headers, mediaType);
        headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") {
            FileName = "SomeName.ext"
        };
    }

    public override Task WriteToStreamAsync(Type t, object value, Stream s, HttpContentHeaders headers, TransportContext context) {
        return Task.Factory.StartNew(() => {
            // TODO code to write to the output stream, flush it but don't explicitly close it
        });
    }
}

这篇关于网页API返回的文本文件,除非垃圾从浏览器栏运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆