FTP内存流和图像控制

时间:2010-12-05 13:39:48

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

嘿我在asp网站上有一个图像控件uri我正在尝试设置ftp下载并将其存储在内存流中然后将其传递给图像控件。

但没有成功,新的uri不能使用photoPath,它看起来并不正确。我的“photoPath”保存了ftps路径名,试图将其传递给我的processrequest。完全迷失了。所有我想做的是选择我的gridview采取ftp路径名+文件名如下所示:ftp://192.168.1.2/Jelly.jpg

已经完成,我已将其存储在字符串名称PhotoPath中。然后执行ftp请求下载将其存储在内存流中,然后将其激活到图像控件中。难道不对吗?

代码:

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) {
    string PhotoPath = "";
    GridViewRow row = GridView1.Rows[GridView1.SelectedIndex]; 
    PhotoPath = row.Cells[5].Text;
}

public void ProcessRequest (HttpContext context) {
    // error on this line due to photopath 
    FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(new Uri(PhotoPath)); 
    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.Clear();
        context.Response.ClearHeaders();
        context.Response.ClearContent();
        context.Response.ContentType = "image/jpeg"; // unsure of this line
        context.Response.BinaryWrite(mStream.GetBuffer());
    }
    catch (WebException wex) {
        //throw new Exception("Unable to locate or access your file.\\nPlease try a different file.");
    }
    catch (Exception ex) {
        throw new Exception("An error occurred: " + ex);
    }
}

public bool IsReusable {
    get  { return false; }
}

此行包含错误:当前上下文中不存在PhotoPath

    public void ProcessRequest (HttpContext context){
// error on this line due to photopath 
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(new Uri(PhotoPath)); 
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = new NetworkCredential("username", "password");

一旦我做了这一点,我需要找到一种方法将它传递给图像控件???

代码的第一部分在我的gridView(ftp路径名和文件名)中获取一个单元格的值,第二部分是我下载该photopath并将其存储在内存流中的方法,而我的最后一部分我不知道如何要做的就是在我的图像控件中显示它。

1 个答案:

答案 0 :(得分:2)

PhotoPath看起来像是GridView1_SelectedIndexChanged方法中的局部变量。您肯定无法从ProcessRequest方法访问它。

我认为你有一些普通的C#学习。

使PhotoPath成为您班级的成员变量以修复它。