简体   繁体   English

如何添加到HttpResponseMessage文件和字符串

[英]how to add to HttpResponseMessage file and string

I have a code that return file with HttpResponseMessage , I want to add the contact also string, how can I do that? 我有一个使用HttpResponseMessage返回文件的代码,我想同时添加联系人字符串,该怎么办?

[HttpPost]
public IHttpActionResult GetPlaylistXml(int playlistId, [FromBody] JObject data)
{
    ....
    var serializer = new XmlSerializer(typeof(playList));
    using (var memStream = new MemoryStream())
    {
        serializer.Serialize(memStream, playList);

        var result = new HttpResponseMessage(HttpStatusCode.OK)
        {
            Content = new ByteArrayContent(memStream.ToArray(), 0, (int)memStream.Length)
        };
        result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
        {
            FileName = playlist.Title + ".xml"
        };
        result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");

        var response = ResponseMessage(result);

        return response;
    }
}

There are several options depending on your preference and possibly limitations based on the existing platform: 根据您的喜好以及基于现有平台的限制,有几种选择:

Option 1 选项1

You can leave your code as is and read the file from the stream and read the file name from the headers (I'd personally recommend this unless there is some limitation preventing you from doing this) 您可以按原样保留代码,并从流中读取文件,并从标头中读取文件名(除非有某些限制,否则我个人建议这样做)

Option 2 选项2

Create a model and return both in json format: 创建一个模型并以json格式返回两者:

[HttpPost]
public IHttpActionResult GetPlaylistXml(int playlistId, [FromBody] JObject data)
{
    ....
    var serializer = new XmlSerializer(typeof(playList));
    using (var memStream = new MemoryStream())
    {
        serializer.Serialize(memStream, playList);

        var returnModel = new 
        {
           Title = playList.Title,
           // either a byte array (which is converted to Base64, or the XML string)
           Playlist = memStream.ToArray() 
        };

        return Json(returnModel);
    }
}

Option 3 选项3

Make your endpoints more REST like and have two endpoints, one for retrieving information about the playlist and one for retrieving the playlist file: 使您的端点更像REST,并具有两个端点,一个端点用于检索有关播放列表的信息,另一个端点用于检索播放列表文件:

[HttpGet]
[Route("playlist/{id}")]
public IHttpActionResult GetPlaylistMetaData(int id)
{
    return Json(new {
        Id = 1,
        Title = "My Playlist",
        TrackCount = 24,
        ...
    });
}

[HttpGet]
[Route("playlist/{id}/file")]
public IHttpActionResult GetPlaylistFile(int id)
{
    ....
    var serializer = new XmlSerializer(typeof(playList));
    using (var memStream = new MemoryStream())
    {
        serializer.Serialize(memStream, playList);

        var result = new HttpResponseMessage(HttpStatusCode.OK)
        {
            Content = new ByteArrayContent(memStream.ToArray(), 0, (int)memStream.Length)
        };
        result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
        {
            FileName = playlist.Title + ".xml"
        };
        result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");

        var response = ResponseMessage(result);

        return response;
    }
}

I hope this helps. 我希望这有帮助。

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

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