Click here to Skip to main content
16,009,847 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
string memberpic = @"ftp://111.111.111.111/abc.jpg";
string ftpUser = "1111";
string ftpPassWord = "11111";
                
FtpWebRequest displayRequest = (FtpWebRequest)WebRequest.Create(memberpic);
displayRequest.Method = WebRequestMethods.Ftp.DownloadFile;
displayRequest.Credentials = new NetworkCredential(ftpUser, ftpPassWord);
               
FtpWebResponse displayResponse = (FtpWebResponse)displayRequest.GetResponse();
                 
Stream responcestream = displayResponse.GetResponseStream();

FileStream fs = new FileStream(memberpic, FileMode.Create);
//it show error msg:The given path's format is not supported.   
byte[] buffer = new byte[102400];
int read = 0;
do
{
    read = responcestream.Read(buffer, 0, buffer.Length);
    fs.Write(buffer, 0, read);
    fs.Flush();
    imgmemberpic.ImageUrl = memberpic;
}
while (!(read == 0));
fs.Flush();
fs.Close();
displayResponse.Close();
responcestream.Close();
Posted
Updated 18-Mar-13 8:03am
v5
Comments
Prasad Khandekar 18-Mar-13 6:58am    
You are using a TextReader for Binary Data.
kangyi.lee 18-Mar-13 9:46am    
i no ideas which one better so i temp put
CHill60 18-Mar-13 7:45am    
At the very least tell us what the error is or how the actual behaviour differs from what you were expecting
kangyi.lee 18-Mar-13 9:48am    
it cant show image, actually i need retrieve image from FTP Server and display on website. thanks

Hello,

You are reading the file and storing it into a byte buffer. The return data will not be a URL and hence the image wont be displayed. What you can try is to save this byte buffer into a disk file, and assign the URL of that file to image control.

For web there is a specification to send inline image data (base64 encoded), but not all browsers implement it. I know Firefox supports it.

Regards,
 
Share this answer
 
GetImage.ashx
// using Handler
]]>

using System;
using System.Web;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Net;
using System.Data.Sql;
using System.Data.SqlClient;


public class GetImage : IHttpHandler {

public string constr = "Data Source=111.111.111.111;Initial Catalog=musicnot_musicnote;Persist Security Info=True;User ID=111;Password=111";
// for mssql connenction

public void ProcessRequest (HttpContext context) {
// for ftp connenction
string FileSaveUri = @"ftp://111.111.111.111/httpdocs/uploadpic/";
string ftpUser = "111";
string ftpPassWord = "111";

FtpWebRequest displayRequest = (FtpWebRequest)WebRequest.Create(new Uri(FileSaveUri + context.Request.QueryString["memberpic"]));
displayRequest.Method = WebRequestMethods.Ftp.DownloadFile;
displayRequest.Credentials = new NetworkCredential(ftpUser, ftpPassWord);

try
{
FtpWebResponse response = (FtpWebResponse)displayRequest.GetResponse();
Stream Stream = response.GetResponseStream();
byte[] bytes = new byte[10240];
int i = 0;
MemoryStream mStream = new MemoryStream();
do
{
i = Stream.Read(bytes, 0, bytes.Length);
mStream.Write(bytes, 0, i);
} while (i != 0);
context.Response.Clear();
context.Response.ClearHeaders();
context.Response.ClearContent();
context.Response.ContentType = "image/jpeg";
context.Response.BinaryWrite(mStream.GetBuffer());
}
catch (WebException wex)
{
}
catch (Exception ex)
{
throw new Exception("An Error Occurred" + ex);

}

}


public bool IsReusable {
get {
return false;
}
}
}


displaypic.aspx.cs
using (SqlConnection condatabase = new SqlConnection(constr))
{
SqlDataAdapter dtacheck;
dtacheck = new SqlDataAdapter("Select * from memberlist where member_id='" + Convert.ToString(Session["username"] + "'"), condatabase);
DataSet dscheck = new DataSet();
dtacheck.Fill(dscheck, "memberlist");

gvpic.DataSource = dscheck.Tables["memberlist"];
gvpic.DataBind();
}


displaypic.aspx.cs
<asp:gridview id="gvpic" runat="server" autogeneratecolumns="False" showheader="False" xmlns:asp="#unknown">
<columns>
<asp:templatefield>
<itemtemplate>
<asp:image runat="server" width="200" height="200" imageurl="<%# " ~="" getimage.ashx?memberpic=" + Eval(" member_pic")="" %&gt;"="" id="memberpic">





Finally can work :) hope can help other ppl solve this problem in furture
 
Share this answer
 
Comments
[no name] 25-Aug-15 0:39am    
Could you please tell me how I can do in windows application ?

Is it possible to show image in picture box without storing in local machine ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900