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

后端 未结 2 1999
一生所求
一生所求 2021-02-18 16:37

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

相关标签:
2条回答
  • 2021-02-18 17:02
    iCalendar iCal = new iCalendar();
    foreach (CalendarItem item in _db.CalendarItems.Where(r => r.Start > DateTime.Now && r.Active == true && r.CalendarID == ID).ToList())
    {
        Event evt = new Event();
        evt.Start = new iCalDateTime(item.Start);
        evt.End = new iCalDateTime(item.End);
        evt.Summary = "Some title";
        evt.IsAllDay = false;
        evt.Duration = (item.End - item.Start).Duration();
        iCal.Events.Add(evt);
    }
    // Create a serialization context and serializer factory. 
    // These will be used to build the serializer for our object. 
    ISerializationContext ctx = new SerializationContext();
    ISerializerFactory factory = new DDay.iCal.Serialization.iCalendar.SerializerFactory();
    // Get a serializer for our object
    IStringSerializer serializer = factory.Build(iCal.GetType(), ctx) as IStringSerializer;
    if (serializer == null) return Content("");
    string output = serializer.SerializeToString(iCal);
    var contentType = "text/calendar";
    var bytes = Encoding.UTF8.GetBytes(output);
    var result = new FileContentResult(bytes, contentType);
    result.FileDownloadName = "FileName.ics";
    return result;
    
    0 讨论(0)
  • 2021-02-18 17:20

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

    fs.Seek(0, 0);
    
    0 讨论(0)
提交回复
热议问题