FTP处理程序页面,从aspx帮助调用字符串以及初始化处理程序页面的方法

时间:2010-12-05 16:26:48

标签: c# .net asp.net c#-4.0 httphandler

嘿伙计们,我试着清理我的上一篇文章,所以就这样了。

我的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.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中:

FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(new Uri(PhotoPath));

我已将我的图像控件ImageUrl设置为〜/ GetImage.ashx并需要一些代码帮助来从我的数据库页面调用处理程序?

任何人都可以提供帮助,因为我已经坚持这么多年了吗?

1 个答案:

答案 0 :(得分:1)

  • 您需要使用查询字符串将PhotoPath传递给ASHX处理程序。然后,您应该从查询字符串中读取参数并将其传递给WebRequest.Create
  • 你不应该吞下例外
  • 您无需清除回复
  • 您应该将FtpWebResponse直接复制到HttpResponse;你不需要一个中间的MemoryStream
  • ContentType属性告诉浏览器您要发送的文件类型。请参阅http://en.wikipedia.org/wiki/MIME_type