1

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.

Martin Prikryl
  • 147,050
  • 42
  • 335
  • 704
Saleh Rezaei
  • 155
  • 4
  • 18
  • I don't think this is exactly what you want? Do you just want to display the images locally? This link may provide some insight on this as you can store it locally on the web client before it gets uploaded and access it. http://stackoverflow.com/questions/4459379/preview-an-image-before-it-is-uploaded – Chitowns24 May 25 '15 at 05:06
  • It's not what i want, i already have the files uploaded on ftp server and want to show them to the user. – Saleh Rezaei May 25 '15 at 05:27
  • Where are the files stored then? The website isn't on the same server as the FTP server? – Chitowns24 May 25 '15 at 05:29
  • No, that's the problem. web site is on another server – Saleh Rezaei May 25 '15 at 05:35

3 Answers3

2

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

S.Krishna
  • 858
  • 10
  • 25
1

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.

Sami Kuhmonen
  • 27,003
  • 7
  • 53
  • 66
0

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:

Draken
  • 3,049
  • 13
  • 32
  • 49
Sharad Kumar
  • 51
  • 1
  • 8
  • No it won't, it will still be shown in the URL link for the image as before. Storing it as a static variable will not suddenly make the password and username secure – Draken Jul 21 '16 at 11:24