2010-12-05 38 views
0

嘿傢伙們,試圖清理我最後的帖子,所以在這裏。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.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

請不要轉發問題。你應該編輯了prev之一。或關閉它。 – 2010-12-05 18:12:36

回答

1
  • 您需要使用查詢字符串將PhotoPath傳遞到ASHX處理函數。然後,你應該從查詢字符串讀取參數,並把它傳遞給WebRequest.Create
  • 你不應該吞下例外
  • 你並不需要清除
  • 您應該直接複製FtpWebResponse到HttpResponse對象的反應;你不需要一箇中間的MemoryStream
  • ContentType屬性告訴瀏覽器你正在發送什麼樣的文件。請參閱http://en.wikipedia.org/wiki/MIME_type
+1

做了什麼? – SLaks 2010-12-05 16:41:22

相關問題