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

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

 

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

 

    public static string returnFilePath(string filePath, int yymxid)
        {
            FtpWebRequest reqFTP;
            try
            {
                string newFilePath = filePath.Replace("", "");
                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();

 

 

 

 

 


 

 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值