如何显示从FTP服务器C#获取的图像

如何显示从FTP服务器C#获取的图像

问题描述:

string memberpic = @"ftp://111.111.111.111/abc.jpg";
string ftpUser = "1111";
string ftpPassWord = "11111";
                
FtpWebRequest displayRequest = (FtpWebRequest)WebRequest.Create(memberpic);
displayRequest.Method = WebRequestMethods.Ftp.DownloadFile;
displayRequest.Credentials = new NetworkCredential(ftpUser, ftpPassWord);
               
FtpWebResponse displayResponse = (FtpWebResponse)displayRequest.GetResponse();
                 
Stream responcestream = displayResponse.GetResponseStream();

FileStream fs = new FileStream(memberpic, FileMode.Create);
//it show error msg:The given path's format is not supported.   
byte[] buffer = new byte[102400];
int read = 0;
do
{
    read = responcestream.Read(buffer, 0, buffer.Length);
    fs.Write(buffer, 0, read);
    fs.Flush();
    imgmemberpic.ImageUrl = memberpic;
}
while (!(read == 0));
fs.Flush();
fs.Close();
displayResponse.Close();
responcestream.Close();

你好,



您正在读取文件并将其存储到字节缓冲区中。返回数据不是URL,因此不会显示图像。您可以尝试将此字节缓冲区保存到磁盘文件中,并将该文件的URL分配给图像控件。



对于web,有一个规范来发送内联图像数据(base64编码),但并非所有浏览器都实现它。我知道Firefox支持它。



问候,
Hello,

You are reading the file and storing it into a byte buffer. The return data will not be a URL and hence the image wont be displayed. What you can try is to save this byte buffer into a disk file, and assign the URL of that file to image control.

For web there is a specification to send inline image data (base64 encoded), but not all browsers implement it. I know Firefox supports it.

Regards,


GetImage.ashx

//使用Handler

]]>



使用System;

使用System.Web ;

使用System.Data;

使用System.Configuration;

使用System.Web;

使用System .Web.Security;

使用System.Web.UI;

使用System.Web.UI.WebControls;

使用System.Web。 UI.WebControls.WebParts;

使用System.Web.UI.HtmlControls;

使用System.IO;

使用System.Net;

使用System.Data.Sql;

使用System.Data.SqlClient;





public class GetImage:IHttpHandler {



public string constr =Data Source = 111.111.111.111; Initial Catalog = musicnot_musicnote; Persist Security Info = True; User ID = 111;密码= 111;

//用于mssql连接



public void ProcessRequest (HttpContext context){

// for ftp connenction

string FileSaveUri = @ftp://111.111.111.111/httpdocs/uploadpic/;

string ftpUser =111;

string ftpPassWord =111;



FtpWebRequest displayRequest =( FtpWebRequest)WebRequest.Create(new Uri(FileSaveUri + context.Request.QueryString [memberpic]));

displayRequest.Method = WebRequestMethods.Ftp.DownloadFile;

displayRequest.Credentials = new NetworkCredential(ftpUser,ftpPassWord);



try

{

FtpWebResponse response =( FtpWebResponse)displayRequest.GetResponse();

Stream Stream = response.GetResponseStream();

byte [] bytes = new byte [10240];

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;

context.Response.BinaryWrite(mStream.GetBuffer());

}

catch(WebException wex)

{

}

catch(exception ex)

{

抛出新的异常(发生错误+ ex);



}



}





public bool IsReusable {

get {

返回false;

}

}

}





displaypic.aspx.cs

使用(SqlConnection condatabase = new SqlConnection(constr))

{

SqlDataAdapter dtacheck;

dtacheck = new SqlDataAdapter(select * from memberlist where member_id =''+ Convert.ToString(Session [username] +''),condatabase);

DataSet dscheck = new DataSet();

dtacheck.Fill(dscheck,memberlist);



gvpic.DataSource = dscheck.Tables [memberlist];

gvpic.DataBind();

}





displaypic.aspx.cs

< asp:gridview id =gvpicrunat =serverautogeneratecolumns =False showheader =Falsexmlns:asp =#unknown>

< columns>

< asp:templatefield>

< itemtemplate>

< asp:image runat =serverwidth =200height =200imageurl =<%#〜=getimage.ashx?memberpic =+ Eval(member_pic)=%& gt;=id =memberpic>










终于可以工作了:)希望可以帮助其他人在未来解决这个问题
GetImage.ashx
// using Handler
]]>

using System;
using System.Web;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Net;
using System.Data.Sql;
using System.Data.SqlClient;


public class GetImage : IHttpHandler {

public string constr = "Data Source=111.111.111.111;Initial Catalog=musicnot_musicnote;Persist Security Info=True;User ID=111;Password=111";
// for mssql connenction

public void ProcessRequest (HttpContext context) {
// for ftp connenction
string FileSaveUri = @"ftp://111.111.111.111/httpdocs/uploadpic/";
string ftpUser = "111";
string ftpPassWord = "111";

FtpWebRequest displayRequest = (FtpWebRequest)WebRequest.Create(new Uri(FileSaveUri + context.Request.QueryString["memberpic"]));
displayRequest.Method = WebRequestMethods.Ftp.DownloadFile;
displayRequest.Credentials = new NetworkCredential(ftpUser, ftpPassWord);

try
{
FtpWebResponse response = (FtpWebResponse)displayRequest.GetResponse();
Stream Stream = response.GetResponseStream();
byte[] bytes = new byte[10240];
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";
context.Response.BinaryWrite(mStream.GetBuffer());
}
catch (WebException wex)
{
}
catch (Exception ex)
{
throw new Exception("An Error Occurred" + ex);

}

}


public bool IsReusable {
get {
return false;
}
}
}


displaypic.aspx.cs
using (SqlConnection condatabase = new SqlConnection(constr))
{
SqlDataAdapter dtacheck;
dtacheck = new SqlDataAdapter("Select * from memberlist where member_id=''" + Convert.ToString(Session["username"] + "''"), condatabase);
DataSet dscheck = new DataSet();
dtacheck.Fill(dscheck, "memberlist");

gvpic.DataSource = dscheck.Tables["memberlist"];
gvpic.DataBind();
}


displaypic.aspx.cs
<asp:gridview id="gvpic" runat="server" autogeneratecolumns="False" showheader="False" xmlns:asp="#unknown">
<columns>
<asp:templatefield>
<itemtemplate>
<asp:image runat="server" width="200" height="200" imageurl="<%# " ~="" getimage.ashx?memberpic=" + Eval(" member_pic")="" %&gt;"="" id="memberpic">





Finally can work :) hope can help other ppl solve this problem in furture