【问题标题】:FTP memory streams and image controlFTP 内存流和图像控制
【发布时间】:2023-03-23 14:25:02
【问题描述】:

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

但是没有成功,新的 uri 不能使用 photoPath,而且看起来不正确。我的“photoPath”包含试图将它传递给我的 processrequest 的 ftps 路径名。完全迷失了。我要做的就是在选择我的 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 路径名和文件名),第二部分是我下载该照片路径并将其存储在内存流中的方法,最后一部分是我不知道如何要做的就是在我的图像控件中显示它。

【问题讨论】:

  • 错误行包含 2 个调用。先把它拆开,然后贴出相关的错误信息。
  • 好的,我编辑了我的帖子,抱歉给您带来了困惑。

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


【解决方案1】:

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

我认为你有一些通用的 C# 学习要做。

让 PhotoPath 成为你的类的成员变量来修复它。

【讨论】:

  • 看起来那个问题的人回答得很好。我不知道我可以添加什么。我认为您需要阅读一些 C# 和 ASP.NET 的通用教程。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-20
  • 2013-11-19
  • 1970-01-01
  • 1970-01-01
  • 2020-07-22
相关资源
最近更新 更多