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             try
15             {
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             finally
61             {
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();注释掉就可以了。。。。。。好蛋疼,折腾了好久呢。。。。。不过结果是好的,偷笑。。。

 

转载于:https://www.cnblogs.com/yes-you-can/p/3977837.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值