HttpContext.Current.Response to Download file in mvc



我在 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实现相同的事情。 谢谢

设置操作结果以返回文件。

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

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

最新更新