2017-07-31 98 views
0

我有下面的代码在asp.netHttpContext.Current.Response下载文件中的MVC

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 samething。 谢谢

+0

您是否试图发布数据到服务器?看看这里:https://stackoverflow.com/questions/13963253/uploading-csv-file-using-c-sharp-asp-net-mvc –

+0

不,我有文件路径,只是想下载 –

+0

什么时候会发生什么你在你的MVC项目中尝试这个代码? – Rahul

回答

0

设置ActionResult返回文件。

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


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