文件名规范不起作用?

时间:2017-10-24 17:37:40

标签: c# asp.net-web-api asp.net-web-api2

我有一个FileResult课程,其灵感来自SO帖子,如下所示:

public class FileResult : IHttpActionResult
{
    private readonly Stream _content;
    private readonly string _contentType;
    private readonly string _filename;
    private readonly HttpStatusCode _status;

    public FileResult(Stream content, string filename, string contentType = null, HttpStatusCode status = HttpStatusCode.OK)
    {
        _filename = filename;
        _contentType = contentType;
        _content = content;
        _status = status;
    }

    public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
    {
        return Task.Run(() =>
            {
                var response = new HttpResponseMessage(_status)
                {
                    Content = new StreamContent(_content)
                };

                var contentType = _contentType ?? MimeMapping.GetMimeMapping(_filename);
                response.Content.Headers.ContentType = new MediaTypeHeaderValue(contentType);
                response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
                {
                    FileName = _filename,
                    Size = _content.Length
                };
                response.Content.Headers.ContentLength = _content.Length;

                return response;
            },
            cancellationToken);
    }
}

我在我的web api控制器中连接了这个:

[Route("~/campaigns/{campaignId}/creativetemplate")]
[HttpGet]
public async Task<IHttpActionResult> GetBulkEditSheetForCampaign(int campaignId)
{
    var sheetStream = await _creativeBulkOperationService.GenerateBulkCreativeEditSpreadsheet(campaignId);
    return new FileResult(sheetStream, $"CreativeEditSheet_{campaignId}.xslx");
}

当我使用我的RESTful客户端调用此文件时,该文件将保存为creativetemplate。我希望它被保存为类似CreativeEditSheet_1234567890.xslx的东西。我做错了什么,或者这是客户端行为?

1 个答案:

答案 0 :(得分:0)

看起来共识是我正确地做到了这一点,但我正在使用的客户端忽略了它......这既是好消息(我没有搞砸任何东西)和坏消息(现在我必须找到一个更好的客户)。

感谢所有调查的人。