zoukankan      html  css  js  c++  java
  • ASP.NET MVC 远程下载文件

    直接看代码吧:

            /// <summary>
            /// 下载文件
            /// </summary>
            /// <param name="filePath">文件地址</param>
            /// <returns></returns>
            public ActionResult DownLoadFile(string filePath)
            {
                if (filePath == null)
                {
                    return null;
                }
                //string UrlString = "http://123.123.123.123/abc/123.pdf";
                int startIndex = filePath.LastIndexOf("/");
                string fileName = filePath.Substring(startIndex + 1);
                byte[] fileData;
                try
                {
                    WebRequest.Create(filePath);
                }
                catch (Exception ex)
                {
                    //To do something
                    return null;
                }
                try
                {
                    using (WebClient client = new WebClient())
                    {
                        fileData = client.DownloadData(filePath);
                        return File(fileData, "text/plain", fileName);
                    }
                }
                catch (Exception ex)
                {
                    //To do something
                    return null;
                }
            }
  • 相关阅读:
    微信小程序入门
    webpack
    模块化开发(1)
    HTML5表单
    移动端入门
    MySQL
    js面向对象与PHP面向对象总结
    PHP
    Git指令
    Redux
  • 原文地址:https://www.cnblogs.com/hzz521/p/5240853.html
Copyright © 2011-2022 走看看