FTP memory streams and image control
Hey I have an image control uri in my asp site I'm trying to set up an ftp download and store it in a memory stream then pass it to the image control.
But no success, new uri cant use photoPath and it just doesnt look right. My "photoPath" holds the ftps pathname trying to pass that to my processrequest. Totally lost. All I'm looking to do is upon selection of my gridview take the ftp path name+filename looks like this : ftp://192.168.1.2/Jelly.jpg
which has been done and I've stored it in the string name PhotoPath. Then do a ftp request to download store it in a memory stream then fire it into a image control. Should'nt be to hard right?
Code:
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; }
}
this line contains the error: PhotoPath does not exist in the current context
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");
once I've did this bit I need to find a way to the pass it to the image control???
The first part of the code takes the value of a cell in my gridView (ftp pathname and filename) the second part is my method to download that photopath and store it in a memory stream and my last part which I dont know how to do is to show it in my image control.
PhotoPath looks like it is a local variable in the GridView1_SelectedIndexChanged method. You for sure can't access it from the ProcessRequest method.
I think you have some general C# learning to do.
Make PhotoPath a member variable of your class to fix it.
精彩评论