2017-07-31 72 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); 
}