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();注釋掉就可以了。。。。。。好蛋疼,折騰了好久呢。。。。。不過結果是好的,偷笑。。。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM