File is empty and I don't understand why. Asp.net mvc FileResult

好久不见. 提交于 2019-12-10 23:00:00

问题


I am trying to use the built in asp.net file result to return a file that I am trying to make through a file stream. I am using Dday.ical to make my calendar for export

    MemoryStream export = new MemoryStream();         
    iCalendarSerializer serializer = new iCalendarSerializer(iCal);
    serializer.Serialize(export,System.Text.Encoding.Default);
    return export;

Here is my actionResult

public ActionResult ExportCalendar()
{
    string userName = User.Identity.Name;
    Guid userId = membershipS.GetUsersId(userName);
    var calendarStream = calendarS.ExportCalendar(userId);
    return File(calendarStream, "text/calendar", "test.ics");       
}

When I download the file it is 0bytes.


回答1:


Try resetting the stream's position:

calendarStream.Position = 0;

That way when the FileResult starts reading from the stream it will read it from the beginning instead of from the end (after which there are obviously no more bytes!).



来源:https://stackoverflow.com/questions/2032196/file-is-empty-and-i-dont-understand-why-asp-net-mvc-fileresult

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!