2017-08-17 19 views
0

MVCでは、次のコードを使用してファイルをダウンロードしました。 ASP.NETのコアで、これを達成する方法は?ASP.NETコアでファイルをダウンロードする方法

HttpResponse response = HttpContext.Current.Response;     
System.Net.WebClient net = new System.Net.WebClient(); 
string link = path; 
response.ClearHeaders(); 
response.Clear(); 
response.Expires = 0; 
response.Buffer = true; 
response.AddHeader("Content-Disposition", "Attachment;FileName=a"); 
response.ContentType = "APPLICATION/octet-stream"; 
response.BinaryWrite(net.DownloadData(link)); 
response.End(); 

答えて

1

あなたのコントローラは、このような、File方法をIActionResultを返し、使用する必要があります:あなたは、ファイルをダウンロードするには、コードの下にしようとすることができ

[HttpGet("download")] 
public IActionResult GetBlobDownload([FromQuery] string link) 
{ 
    var net = new System.Net.WebClient(); 
    var data = net.DownloadData(link); 
    var content = new System.IO.MemoryStream(data); 
    var contentType = "APPLICATION/octet-stream"; 
    var fileName = "something.bin"; 
    return File(content, contentType, fileName); 
} 
1

。それは返す必要がありますFileResult

public ActionResult DownloadDocument() 
{ 
string filePath = "your file path"; 
string fileName = ""your file name; 

byte[] fileBytes = System.IO.File.ReadAllBytes(filePath); 

return File(fileBytes, "application/force-download", fileName); 

} 
関連する問題