在 ASP.NET 页面上直接显示来自 FTP 的图片
Show image directly from FTP on ASP.NET page
我有一个 Web 应用程序,我想在其中直接从 FTP 服务器向用户显示图像。只要图像控件只获取图像 URL 而不是图像,我就必须这样做:
newArticle.ImageUrl = "ftp://" + FTPUser + ":" + FTPPassword + "@" + FTPServer + dtLatestArticles.Rows[i]["PictureUrl"].ToString();
这意味着每个人都有 ftp 服务器的用户名和密码。知道如何以其他方式做到这一点吗?
我不想也不能将文件保存在运行网页的服务器上,因为缺少 space。
如果 FTP 服务器需要身份验证,您无法隐藏凭据。
一种方法是在您的网站上添加一个代理类型处理程序,它将获取 URL 上的图像名称,并从 FTP 服务器检索文件并直接发送给用户。这样只有您的代码才有凭据,如果没有 space.
,则无需将文件保存在其他任何地方
请注意,这会使您的服务器流量增加一倍,一次进一次出
更好的方法是找到一个具有足够容量和可能性的服务器来为文件提供 HTTP 服务。
我认为你应该采取的方法是
- 创建一个处理程序,比如 FetchImage.ashx
- 此文件中的代码应使用用户名和密码从 FTP 获取图像(示例见下文)
- 根据您的图片将响应内容类型设置为 PNG / JPEG
- 发送你的图片
FetchImage.ashx.cs 的示例代码
var webClient = new WebClient();
byte[] imageBytes = webClient.DownloadData("ftp://server/image.png");
context.Response.Buffer = true;
context.Response.Charset = "";
context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
context.Response.ContentType = "image/png";
context.Response.AddHeader("content-disposition", "attachment;filename=Image.png");
context.Response.BinaryWrite(imageBytes);
正在调用这个文件
<img src="FetchImage.ashx" />
对此代码的改进是将文件名发送到 FetchImage.ashx 并提供相同的服务。在此示例中,我只是向您展示如何完成此操作
您可以做的是创建一个 class,您将在其中定义一些静态变量,例如 ftpUsername
、ftpPassword
等。在需要连接时将您的用户名和密码保存在那里ftp 服务器只需将这些静态变量直接调用到您的代码中。这将帮助您隐藏您的实际用户名和密码。
public static class ClassName
{
static internal string connString = "Server=localhost;Database=db_criminalact;UID=root;Password='';CharSet=utf8;Connection Timeout=10;";
static internal string ftpServer = "127.0.0.1"; //"192.168.3.3";
static internal string ftpFolder = "criminalact_pic";
static internal string ftpUsername = "ftpserver";
static internal string ftpPassword = "pswadmin";
}
关于你直接从ftp显示图片的问题你可以参考:
我有一个 Web 应用程序,我想在其中直接从 FTP 服务器向用户显示图像。只要图像控件只获取图像 URL 而不是图像,我就必须这样做:
newArticle.ImageUrl = "ftp://" + FTPUser + ":" + FTPPassword + "@" + FTPServer + dtLatestArticles.Rows[i]["PictureUrl"].ToString();
这意味着每个人都有 ftp 服务器的用户名和密码。知道如何以其他方式做到这一点吗?
我不想也不能将文件保存在运行网页的服务器上,因为缺少 space。
如果 FTP 服务器需要身份验证,您无法隐藏凭据。
一种方法是在您的网站上添加一个代理类型处理程序,它将获取 URL 上的图像名称,并从 FTP 服务器检索文件并直接发送给用户。这样只有您的代码才有凭据,如果没有 space.
,则无需将文件保存在其他任何地方请注意,这会使您的服务器流量增加一倍,一次进一次出
更好的方法是找到一个具有足够容量和可能性的服务器来为文件提供 HTTP 服务。
我认为你应该采取的方法是
- 创建一个处理程序,比如 FetchImage.ashx
- 此文件中的代码应使用用户名和密码从 FTP 获取图像(示例见下文)
- 根据您的图片将响应内容类型设置为 PNG / JPEG
- 发送你的图片
FetchImage.ashx.cs 的示例代码
var webClient = new WebClient();
byte[] imageBytes = webClient.DownloadData("ftp://server/image.png");
context.Response.Buffer = true;
context.Response.Charset = "";
context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
context.Response.ContentType = "image/png";
context.Response.AddHeader("content-disposition", "attachment;filename=Image.png");
context.Response.BinaryWrite(imageBytes);
正在调用这个文件
<img src="FetchImage.ashx" />
对此代码的改进是将文件名发送到 FetchImage.ashx 并提供相同的服务。在此示例中,我只是向您展示如何完成此操作
您可以做的是创建一个 class,您将在其中定义一些静态变量,例如 ftpUsername
、ftpPassword
等。在需要连接时将您的用户名和密码保存在那里ftp 服务器只需将这些静态变量直接调用到您的代码中。这将帮助您隐藏您的实际用户名和密码。
public static class ClassName
{
static internal string connString = "Server=localhost;Database=db_criminalact;UID=root;Password='';CharSet=utf8;Connection Timeout=10;";
static internal string ftpServer = "127.0.0.1"; //"192.168.3.3";
static internal string ftpFolder = "criminalact_pic";
static internal string ftpUsername = "ftpserver";
static internal string ftpPassword = "pswadmin";
}
关于你直接从ftp显示图片的问题你可以参考: