首页 > 代码库 > img标签 加载FTP的图片 C#

img标签 加载FTP的图片 C#

好吧,我是菜鸟,这是我今天遇到的问题,什么也不会,得高人指点

1.使用FtpWebRequest下载图片,以流存贮

2.在ashx文件里面直接已流方式(HttpContext.Current.Response.ContentType = "image/jpeg";)输出图片

3.页面的img标签的src指向ashx文件

还是贴代码实际点

 1   public void ViewFTPImageFile(string strFullPath) 2         { 3              4             //定义FTP请求对象 5             FtpWebRequest ftpRequest = null; 6             //定义FTP响应对象 7             FtpWebResponse ftpResponse = null; 8  9             //存储流10             FileStream saveStream = null;11             //FTP数据流12             Stream ftpStream = null;13             StreamReader reader = null;14             try15             {16                 //生成FTP请求对象17                 ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(strFullPath));18 19                 //设置下载文件方法20                 ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;21 22 23                 //设置文件传输类型24                 ftpRequest.UseBinary = true;25                 ftpRequest.KeepAlive = false;26                 ftpRequest.UsePassive = true;27                 //设置登录FTP帐号和密码28                 ftpRequest.Credentials = new NetworkCredential(entity.User_Name, entity.Pwd);29 30                 ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();                //获取FTP响应流对象31                 ftpStream = ftpResponse.GetResponseStream();32 33                 int bufferSize = 2048;34 35                 byte[] buffer = new byte[bufferSize];36 37 38                 HttpContext.Current.Response.ContentType = "image/jpeg";39                 HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.Public);40                 HttpContext.Current.Response.BufferOutput = false;41 42                 //while ((readCount = ftpStream.Read(buffer, 0, 2048)) > 0)43                 //{44                 ftpStream.Read(buffer, 0, bufferSize);45 46                 HttpContext.Current.Response.BinaryWrite(buffer);47                 HttpContext.Current.Response.Flush();48                 //}49                 ftpStream.Close();50 51                 //HttpContext.Current.Response.Close();52 53 54             }55             catch (Exception ex)56             {57                 Console.WriteLine(ex.Message);58 59             }60             finally61             {62                 if (ftpStream != null)63                 {64                     ftpStream.Close();65                 }66 67                 if (saveStream != null)68                 {69                     saveStream.Close();70                 }71 72                 if (ftpResponse != null)73                 {74                     ftpResponse.Close();75                 }76             }77         }
View Code

 

ashx就直接使用方法,可以顺便传个参数啥的

 

然后页面直接用就行了

1  <img src="../Ashx/Image.ashx>" />
View Code

我擦嘞,然后就可以了,心里有些小激动,但是发现chrome不能用,报错net::ERR_INCOMPLETE_CHUNKED_ENCODING ,至于原因嘛,很高端,不过解决办法很简单

当当当~像这样 //HttpContext.Current.Response.Close();注释掉就可以了。。。。。。好蛋疼,折腾了好久呢。。。。。不过结果是好的,偷笑。。。

 

img标签 加载FTP的图片 C#