带MemoryStream的FileResult给出空结果..问题是什么?

Mic*_*ski 20 c# asp.net-mvc

我正在使用一个库来生成ics文件(iCalendar或RFC 2445,或者你称之为),这些库将内容序列化为MemoryStream,或者实际上是任何类型的流.

这是我的一大堆代码:

    public ActionResult iCal(int id) {

        MyApp.Event kiEvt = evR.Get(id);

        // Create a new iCalendar
        iCalendar iCal = new iCalendar();

        // Create the event, and add it to the iCalendar
        DDay.iCal.Components.Event evt = iCal.Create<DDay.iCal.Components.Event>();

        // Set information about the event
        evt.Start = kiEvt.event_date;
        evt.End = evt.Start.AddHours(kiEvt.event_duration); // This also sets the duration            
        evt.Description = kiEvt.description;
        evt.Location = kiEvt.place;
        evt.Summary = kiEvt.title;

        // Serialize (save) the iCalendar
        iCalendarSerializer serializer = new iCalendarSerializer(iCal);


        System.IO.MemoryStream fs = new System.IO.MemoryStream();

        serializer.Serialize(fs, System.Text.Encoding.UTF8);

        return File(fs, "text/calendar", "MyApp.wyd."+kiEvt.id+".ics");
    }
Run Code Online (Sandbox Code Playgroud)

我的问题是fs包含一些内容,但控制器返回空文件 - 具有正确的mimetype和filename.我很可能错过了一些关于流处理的东西,但无法弄清楚是什么.

有人可以帮帮我吗?提前致谢.

Mat*_*ton 41

只是一个猜测:你需要寻找你回来之前回到流的开始?

fs.Seek(0, 0);
Run Code Online (Sandbox Code Playgroud)

  • 答对了!非常感谢你. (2认同)