Saleh Rezaei
Saleh Rezaei

Reputation: 159

Show image directly from FTP on ASP.NET page

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

Answers (3)

Sharad Kumar
Sharad Kumar

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

S.Krishna
S.Krishna

Reputation: 876

I think the approach you should take is

  1. Create a handler, say FetchImage.ashx
  2. The code in this file should fetch the image from FTP using username and password (see below for sample)
  3. Set the response content type to PNG / JPEG based on your image
  4. Send your image

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

Sami Kuhmonen
Sami Kuhmonen

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

Related Questions