环境:net framework 4.5.2


    1. /// <summary>
    2. /// 文件别名下载
    3. /// </summary>
    4. /// <param name="filePath">/File/NeedsDeliver/20211222/8c6f2254-c605-4d92-b916-7bebac6ae162.zip</param>
    5. /// <param name="name">xxx.zip</param>
    6. [HttpGet]
    7. [Route("FileDown/")]
    8. public void FileDown(string filePath, string name) {
    9. HttpResponse response = HttpContext.Current.Response;
    10. if (!string.IsNullOrEmpty(filePath)) {
    11. filePath = HttpContext.Current.Server.MapPath(filePath);
    12. response.ClearHeaders();
    13. response.Clear();
    14. response.Expires = 0;
    15. response.Buffer = true;
    16. response.AddHeader("Accept-Language", "zh-tw");
    17. //string name = System.IO.Path.GetFileName(filePath);
    18. using (System.IO.FileStream files = new System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read)) {
    19. byte[] byteFile = new byte[1];
    20. if (files.Length > 0) {
    21. byteFile = new byte[files.Length];
    22. }
    23. files.Read(byteFile, 0, byteFile.Length);
    24. files.Close();
    25. response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(name, System.Text.Encoding.UTF8));
    26. response.ContentType = "application/octet-stream;charset=gbk";
    27. response.BinaryWrite(byteFile);
    28. response.End();
    29. }
    30. }
    31. }
    1. window.open('当前接口地址')