FTP 处理程序页面,从 aspx 帮助中调用字符串以及初始化处理程序页面的方法
嘿伙计们,试图清理我的上一篇文章,所以就到这里了。
我的 asp.net 项目中有一个处理程序页面,它“尝试”处理 ftp 下载请求。
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;
}
}
}
}
在我的包含 gridview 的页面上,我有一种方法可以从我的一个单元格中提取数据,该单元格包含我试图在图像控件中显示的图像的 ftp 路径和文件名:
Database.aspx。 程序
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;
}
}
我已将图像控件 ImageUrl 设置为 ~/GetImage.ashx ,
在我的处理程序页面中,我想调用字符串“PhotoPath”并将其放入处理程序页面中的 ftwwebrequest 中:
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(new Uri(PhotoPath));
并且需要一些代码帮助来调用处理 我的数据库页面?
任何人都可以帮忙吗,因为我已经坚持这个问题很多年了?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
WebRequest.Create
您ContentType
属性告诉浏览器您要发送的文件类型。请参阅http://en.wikipedia.org/wiki/MIME_typeWebRequest.Create
ContentType
property tells the browser what kind of file you're sending. See http://en.wikipedia.org/wiki/MIME_type