tags:

views:

43

answers:

3

Hello,

I'm uploading images to a folder and I want to display each new image on a web page every time it is saved in the folder.

I can display the initial image, which is already in the folder, and I can also detect when a new image is saved into the folder but I don't know how to display the new images.

I'm new to web development. Can somebody help me?

Here's the code:

public partial class _Default : System.Web.UI.Page
{

    string DirectoryPath = "C:\\Users\\Desktop\\PhotoUpload\\Uploads\\";

    protected void Page_Load(object sender, EventArgs e)
    {

        FileSystemWatcher watcher = new FileSystemWatcher();

        try
        {
            watcher.Path = DirectoryPath;
            watcher.Created += new FileSystemEventHandler(watcher_Created);
            watcher.EnableRaisingEvents = true;

            Image image = Image.FromFile(DirectoryPath + "inicial.jpg");
            MemoryStream ms = new MemoryStream();
            image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            byte[] im = ms.ToArray();

            Context.Response.ContentType = "Image/JPG";
            Context.Response.BinaryWrite(im);


        }
        catch (Exception ex)
        {
        }

    }

    void watcher_Created(object sender, FileSystemEventArgs e)
    {
        Console.WriteLine("File Created: Name: " + e.Name);

        try
        {

          //How to display new image?  


        }
        catch (Exception ex)
        {
        }
    }

}
A: 

Hi RuiT,

I think you are missing an important point when it comes to ASP.NET development. What you are trying to do is using a FileWatcher on the server side within a page.

They way ASP.NET works is that it runs through its pipeline to process a request and when it's done (has rendered the output (html) ready to send to the browser) it destroys all instances created within the request (including your page and the contained FileWatcher).

If you want to update a page in the browser you will be forced to use some client scripting (jQuery very much recommended) to implement polling which sends a request (preferrably ajax) back to the server asking for newly added images.

I hope that helps you?

Michael

Michael Ulmann
A: 

As Michael has pointed out, the partial solution you're proposing in your question isn't really appropriate for a web application.

The thing to keep in mind here is the difference between code running on the server when a page is requested and code running in the client's browser.

In order to have an automatically updating web page, you'll need some kind of client-side mechanism to poll the server and check for new images. This will likely be in the form of some sort of periodic AJAX request. The prototype js framework offers this functionality through it's Ajax.PeriodicalUpdater.

kristian
A: 

Ok, thanks for your explanation and suggestions.

RuiT