【发布时间】:2011-05-11 17:33:14
【问题描述】:
我正在将图像上传到一个文件夹,并且我希望每次将每个新图像保存在该文件夹中时都将其显示在网页上。
我可以显示初始图像,它已经在文件夹中,我也可以检测新图像何时保存到文件夹中,但我不知道如何显示新图像。
我是网络开发的新手。有人可以帮帮我吗?
代码如下:
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)
{
}
}
}
【问题讨论】:
-
您希望网页在添加新图像时自动更新,还是在加载时仅显示最近添加的图像?
-
嗨,我希望页面在添加新图像时自动更新。那可能吗?谢谢。