img標籤 加載FTP的圖片 C#

好吧,我是菜鳥,這是我今天遇到的問題,什麼也不會,得高人指點chrome

1.使用FtpWebRequest下載圖片,以流存貯ide

2.在ashx文件裏面直接已流方式(HttpContext.Current.Response.ContentType = "image/jpeg";)輸出圖片spa

3.頁面的img標籤的src指向ashx文件3d

仍是貼代碼實際點code

 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就直接使用方法,能夠順便傳個參數啥的對象

 

而後頁面直接用就好了blog

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

我擦嘞,而後就能夠了,內心有些小激動,可是發現chrome不能用,報錯net::ERR_INCOMPLETE_CHUNKED_ENCODING ,至於緣由嘛,很高端,不過解決辦法很簡單圖片

噹噹噹~像這樣 //HttpContext.Current.Response.Close();註釋掉就能夠了。。。。。。好蛋疼,折騰了很久呢。。。。。不過結果是好的,偷笑。。。string

相關文章
相關標籤/搜索