问题描述
嘿伙计们,我试图清理我的上一篇文章,所以就这样吧.
Hey guys, tryed to clean up my last post so here it goes.
我的 asp.net 项目中有一个处理程序页面正在尝试"处理 ftp 下载请求.
I have a handler page in my asp.net project that is "trying" to handle a ftp download request.
GetImage.ashx.cs
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 路径和文件名:
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:
数据库.aspx.cs
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;
}
}
}
在我的处理程序页面中,我想调用字符串PhotoPath"并将其放置在我的处理程序页面中的 ftwwebrequest 中:
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));
我已经将我的图像控件 ImageUrl 设置为 ~/GetImage.ashx 并且需要一些代码帮助以从我的数据库页面调用处理程序?
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?
推荐答案
- 您需要使用查询字符串将 PhotoPath 传递给 ASHX 处理程序.然后您应该从查询字符串中读取参数并将其传递给
WebRequest.Create
- 你不应该吞下异常
- 您不需要清除响应
- 您应该将 FtpWebResponse 直接复制到 HttpResponse;你不需要中间的 MemoryStream
ContentType
属性告诉浏览器您发送的文件类型.请参阅http://en.wikipedia.org/wiki/MIME_type- You need to pass PhotoPath to the ASHX handler using the querystring. You should then read the parameter from the querystring and pass it to
WebRequest.Create
- You should not swallow exceptions
- You don't need to clear the response
- You should copy the FtpWebResponse directly to the HttpResponse; you don't need an intermediate MemoryStream
- The
ContentType
property tells the browser what kind of file you're sending. See http://en.wikipedia.org/wiki/MIME_type
这篇关于FTP 处理程序页面,从 aspx 调用字符串帮助初始化处理程序页面的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!