【问题标题】:HttpContext.Current.Response to Download file in mvcHttpContext.Current.Response 在 mvc 中下载文件
【发布时间】:2017-07-31 12:20:17
【问题描述】:

我在 asp.net 中有以下代码

 GetObjectResponse resp = Media.ReadS3Object("data", strKey);
            StreamReader sr = new StreamReader(resp.ResponseStream);

            //Prompt download:
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ContentType = resp.ContentType; // "text/csv;";
            HttpContext.Current.Response.AppendHeader("Content-Disposition", string.Format("attachment; filename={0}", fname));
            HttpContext.Current.Response.BinaryWrite(UTF8Encoding.Convert(Encoding.UTF8, Encoding.Unicode, Encoding.UTF8.GetBytes(sr.ReadToEnd())));
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.End();

它下载文件,我怎样才能通过mvc实现相同的功能。 谢谢

【问题讨论】:

标签: c# asp.net-mvc


【解决方案1】:

设置 ActionResult 以返回文件。

public ActionResult DownloadFile(string strKey) {
    GetObjectResponse resp = Media.ReadS3Object("data", strKey);
    StreamReader sr = new StreamReader(resp.ResponseStream);


    return File(sr, resp.ContentType, fname);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-28
    • 2015-02-18
    • 1970-01-01
    • 2014-09-26
    相关资源
    最近更新 更多