The IMG Tag loads the FTP image C #

Source: Internet
Author: User

Okay, I'm a cainiao. This is a problem I encountered today. I don't know anything. I have to give advice from someone else.

1. Use ftpwebrequest to download images for stream Storage

2. In the ashx file, the output image is directly streamed (httpcontext. Current. response. contenttype = "image/JPEG ";).

3. the SRC of the IMG label on the page points to the ashx file.

Or paste the actual code points

1 Public void viewftpimagefile (string strfullpath) 2 {3 4 // define the FTP request object 5 ftpwebrequest ftprequest = NULL; 6 // define the FTP Response object 7 ftpwebresponse ftpresponse = NULL; 8 9 // storage stream 10 filestream savestream = NULL; 11 // FTP data stream 12 stream ftpstream = NULL; 13 streamreader reader = NULL; 14 try15 {16 // generate the FTP request object 17 ftprequest = (ftpwebrequest) ftpwebrequest. create (New uri (strfullpath); 18 19 // you can specify 20 ftprequest to download a file. method = Webrequestmethods. FTP. downloadfile; 21 22 23 // set the file transfer type to 24 ftprequest. usebinary = true; 25 ftprequest. keepalive = false; 26 ftprequest. usepassive = true; 27 // set the FTP Logon account and password 28 ftprequest. credentials = new networkcredential (entity. user_name, entity. PWD); 29 30 ftpresponse = (ftpwebresponse) ftprequest. getresponse (); // get the FTP Response Stream object 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 (buff Er); 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 directly uses the method. You can pass a parameter by the way.

 

Then the page can be used directly.

1  
View code

I was a little excited, but I found that chrome was not usable and reported the error net: err_incomplete_chunked_encoding. As for the reason, it was very high, but the solution was very simple.

Dangdang ~ In this case, you can annotate/httpcontext. Current. response. Close (); and so on ...... It hurts, and it has been tossing for a long time ..... But the result is good. You have to smile...

 

The IMG Tag loads the FTP image C #

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.