文件是空的,我不明白为什么。 Asp.net MVC FileResult [英] File is empty and I don't understand why. Asp.net mvc FileResult

查看:219
本文介绍了文件是空的,我不明白为什么。 Asp.net MVC FileResult的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用内置的asp.net文件结果返回,我想通过一个文件流,使一个文件。我使用Dday.ical使我出口日历

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;

下面是我的ActionResult

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

当我下载的文件是0bytes。

When I download the file it is 0bytes.

推荐答案

尝试重置流的位置是:

calendarStream.Position = 0;

这样,当 FileResult 开始从流中读取它从开始而不是从末端阅读(之后有明显的字节数!)。

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!).

这篇关于文件是空的,我不明白为什么。 Asp.net MVC FileResult的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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