Hey guys, tryed to clean up my last post so here it goes.
I have a handler page in my asp.net project that is "trying" to handle a ftp download request.
GetImage.ashx.cs
public class GetImage : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
{
// method for calling PhotoPath from Database.aspx.cs
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(new Uri());
// (new Uri) I would like to just store "photopath" in here as it has the ftp plus the filename in it
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = new NetworkCredential("username", "password");
try
{
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream stream = response.GetResponseStream();
byte[] bytes = new byte[2048];
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.ClearHeaders();
context.Response.ClearContent();
context.Response.ContentType = "image/jpeg";
// Response.Headers.Add("Content-Type", "image/jpeg"); this is from the database.aspx page not sure if its correct
context.Response.BinaryWrite(mStream.GetBuffer());
}
catch (WebException wex)
{
}
catch (Exception ex)
{
throw new Exception("An error occurred: " + ex);
}
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
On my page that holds my gridview I have a method to extract data from one of my cells, this cell contains the ftp path and filename of the image im trying to show in my image control:
Database.aspx.cs
protected void Page_Load(object sender, EventArgs e){
Response.Headers.Add("Content-Type", "image/jpeg");
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
string PhotoPath = "";
// Get the currently selected row. Because the SelectedIndexChanging event
// occurs before the select operation in the GridView control, the
// SelectedRow property cannot be used. Instead, use the Rows collection
// and the NewSelectedIndex property of the e argument passed to this
// event handler.
GridViewRow row = GridView1.Rows[GridView1.SelectedIndex];
PhotoPath = row.Cells[5].Text;
// how do I send photopath to my handler page?
//TextBox1.Text = PhotoPath;
}
}
}
In my handler page I want to call the string "PhotoPath" and place it in my ftwwebrequest in my handler page:
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(new Uri(PhotoPath));
Ive set my image control ImageUrl to ~/GetImage.ashx and will need some help with code to call the handler from my database page?
Can any one help because ive been stuck on this for ages?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…