鈤,集成封装多态!

C#FTP下载文件返回文件路径或者byte[]

FTP下载文件返回文件路径和返回byte[]方法,以PDF为例


FTP下载文件保存到本地并返回文件路径

    public static string returnFilePath(string filePath, int yymxid)
        {
            FtpWebRequest reqFTP;
            try
            {
                string newFilePath = filePath.Replace("http://img.kktijian.com/gr/pic/jk/ak/", "ftp://112.74.74.226:39618/gr/pic/jk/ak/");
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(newFilePath));
                reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
                reqFTP.UseBinary = true;                              
                reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
                FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
                Stream ftpStream = response.GetResponseStream();            
                long cl = response.ContentLength;
                string returnPath = @"D:\" + yymxid .ToString()+ ".PDF";
                if (File.Exists(returnPath))
                {
                    //如果存在则删除
                    File.Delete(returnPath);
                }
                Stream responseStream = response.GetResponseStream();
                FileStream filestream = new FileStream(returnPath, FileMode.Create, FileAccess.Write);
                int buflength = 8196;
                byte[] buffer = new byte[buflength];
                int bytesRead = 1;             
                while (bytesRead != 0)
                {
                    bytesRead = responseStream.Read(buffer, 0, buflength);
                    filestream.Write(buffer, 0, bytesRead);
                }
                filestream.Close();

                return returnPath;
            }
            catch (Exception)
            {
                return "";
            }
        }


C#下载文件返回byte[]


       public static byte[] DownLoad(string filePath)
        {

            FtpWebRequest reqFTP;
            try
            {
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(filePath));
                reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
                reqFTP.UseBinary = true;
                reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
                var response = (FtpWebResponse)reqFTP.GetResponse();
                Stream ftpStream = response.GetResponseStream();
                var ms = new MemoryStream();
                byte[] buffer = new byte[1024];
                while (true)
                {
                    int sz = ftpStream.Read(buffer, 0, 1024);
                    if (sz == 0) break;
                    ms.Write(buffer, 0, sz);
                }
                ftpStream.Close();
                ms.Position = 0;

                var bytes = new byte[(int)ms.Length];
                ms.Read(bytes, 0, bytes.Length);
                ms.Close();

                return bytes;
           

            }
            catch (Exception err)
            {
                return null;
            }
        }


mvc中输出 byte[]

  public FileResult AkbgDetail()
        {
string filePath = Common.FtpHelper.returnFilePath(GetFilePath,yymxid);
                FileStream pFileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                byte[] pReadByte = new byte[0];
                BinaryReader r = new BinaryReader(pFileStream);
                r.BaseStream.Seek(0, SeekOrigin.Begin);    //将文件指针设置到文件开始
                pReadByte = r.ReadBytes((int)r.BaseStream.Length);
                var fileContent = File(pReadByte, HttpUtility.UrlEncode("报告.pdf", System.Text.Encoding.UTF8));
                pFileStream.Close();
                if (System.IO.File.Exists(filePath))
                {
                    //如果存在则删除
                    System.IO.File.Delete(filePath);
                }
                string userClient = Request.UserAgent;
                if (userClient.ToLower().Contains("iphone") || userClient.ToLower().Contains("ipad") || userClient.Contains("windows nt") || (userClient.Contains("windows nt") && userClient.Contains("compatible; msie 9.0;")))
                {
                    return fileContent;
                }
                else
                {
                    return File(pReadByte, "application/pdf", "报告.pdf");
                }

}


WEBFORM中输出byte[]:

      Response.ContentType = "application/pdf";
                Response.AddHeader("Content-Disposition", "inline;  filename=" + HttpUtility.UrlEncode("demo.PDF", System.Text.Encoding.UTF8));
                Response.BinaryWrite(bytes);
                Response.Flush();
                Response.Clear();
                Response.Close();







阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。如果你要转载可能不能变为你的原创! https://blog.csdn.net/for12/article/details/68066717
个人分类: c#开发
想对作者说点什么? 我来说一句

使用C#.NET实现FTP文件下载

2008年10月30日 80KB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭