带有MemoryStream的FileResult给出空结果..什么问题? [英] FileResult with MemoryStream gives empty result .. what's the problem?

查看:173
本文介绍了带有MemoryStream的FileResult给出空结果..什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm generating ics files ( iCalendar or RFC 2445 or however you call them) using a library that serializes the ical contents into a MemoryStream, or actually any type of stream.

这是我的大部分代码:

    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");
    }

我的问题是fs包含一些内容,但是控制器返回空文件-具有正确的mimetype和文件名.我很可能在流处理方面缺少一些东西,但无法弄清楚是什么.

My problem is that fs contains some content, but the controller returns empty file - with proper mimetype and filename. I'm most probably missing something with the stream handling but can't figure out what.

有人可以在这里帮助我吗?预先感谢.

Can anybody help me out here? Thanks in advance.

推荐答案

只是一个猜测:您是否需要

Just a guess: Do you need to Seek back to the start of the stream before you return it?

fs.Seek(0, 0);

这篇关于带有MemoryStream的FileResult给出空结果..什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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