Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
811 views
in Technique[技术] by (71.8m points)

c# - FileResult with MemoryStream gives empty result .. what's the problem?

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.

Here's my chunk of code:

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

fs.Seek(0, 0);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...