Reputation: 159
I have a web app where I want to display images to the user directly from an FTP server. As long as an Image control only takes image URL and not an image, I have to do it this way:
newArticle.ImageUrl = "ftp://" + FTPUser + ":" + FTPPassword + "@" + FTPServer + dtLatestArticles.Rows[i]["PictureUrl"].ToString();
This means that everybody has the user and password for the ftp server. Any idea how to do it in another way?
I don't want and can't save the files on the server that runs the web page because of lack of space.
Upvotes: 1
Views: 4557
Reputation: 61
What you can do is create a class where you will define some static variables like ftpUsername
, ftpPassword
etc. Save your username and password there and whenever you need to connect to the ftp server just call those static variables directly into your code. This will help you to hide your actual username and password.
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";
}
AND for your problem of displaying image directly from ftp you can refer to:
Upvotes: 0
Reputation: 876
I think the approach you should take is
Sample Code of 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);
Calling this file
<img src="FetchImage.ashx" />
Improvements to this code would be to send the filename to FetchImage.ashx and serve the same. In this sample I am just showing you how this could be done
Upvotes: 2
Reputation: 31173
If the FTP server requires authentication, you can't hide the credentials.
One way would be to add a proxy type handler on your site which would get the name of the image on the URL and would retrieve the file from the FTP server and send it directly to the user. This way only your code would have the credentials and there is no need to save the files anywhere else, if there is no space.
Note that this will increase your server's traffic by double of the amount of images, once in and once out
A better way would be to find a server with enough capacity and possibility for HTTP serving of the files.
Upvotes: 1