Display images from server folder
-
Hi ALL, I have folder in Server which contains some images. On page load I need to get the images and display them. Can any one help mw how can I solve this Thanks, Siddiqali
You have to make a path to your folder, and use DirectoryInfo and FileInfo to Get the contents of the folder into the fileinfo object. Then loop through fileinfo, and run a filter on the extension to pick out the images, plus filter out the other junk in the folder, like thumbs.db.
Dim fb_LibraryPath As String = Nothing
fb_LibraryPath = Context.Server.MapPath("\Product\images\Library\thumbs\")Dim diFiles As DirectoryInfo = New DirectoryInfo(fb_LibraryPath)
Dim fileInfo() As FileInfo = diFiles.GetFiles("*.*", SearchOption.TopDirectoryOnly)If fileInfo.Length() > 0 Then
For idx As Integer = 0 To fileInfo.Length() - 1
Dim fileName As String = fileInfo(idx).Name
If Not fileName.ToLower = "thumbs.db" Then
'Create a image element by code and set the src
End ifnext
-
Hi ALL, I have folder in Server which contains some images. On page load I need to get the images and display them. Can any one help mw how can I solve this Thanks, Siddiqali
Code To upload Your Image to folder
YourFunctionToUploadImage()
{
//Check FileUpload controll has File Or not
if (ImageFileUploadControll.HasFile)
{
string fileExtention = System.IO.Path.GetExtension(ImageFileUploadControll.FileName);
//Check For Valid Extension
if (fileExtention.ToLower() != ".jpg" && fileExtention.ToLower() != ".jpeg" && fileExtention.ToLower() != ".png")
{
lblUploadstatus.Text = "Invalid File Choose .jpg/.jpeg/.png/.gif";
}
else
{
//check if file already Exists in Folder
if (File.Exists(Server.MapPath("~/Folder_Name/" + ImageFileUploadControll.FileName)))
{
lblUploadstatus.Text = "File Already Exists. Rename filename";
}
else
{
//To Save Image To your Specific Location.
//srever.mappath takes us to Folder which containing our application
ImageFileUploadControll.SaveAs(Server.MapPath("~/Folder_Name/" + ImageFileUploadControll.FileName));
lblUploadstatus.Text = "Upload Status : File Uploaded Successfully";
}
}}
else
{
lblUploadstatus.Text = "Please Select File";
}
}Code To Retrive Images form Folder
PageLoadEvent()
{
//use DirectoryInfo to get all imageFile's Information
DirectoryInfo dir = new DirectoryInfo(Server.MapPath("~/Folder_Name/"));
FileInfo[] fi = dir.GetFiles();//Now you just need to create new image control each time and add ImageUrl to it foreach(FileInfo f in fi) { string imageUrl = f.ToString(); Image img = new Image(); img.ImageUrl = "~/Folder\_Name/"+"imageUrl"; }
}
Hope This Help
Pratik M. Bhuva
-
Hi ALL, I have folder in Server which contains some images. On page load I need to get the images and display them. Can any one help mw how can I solve this Thanks, Siddiqali
Code To Upload Image //check whether FileUpload control has file or not
if (FileUpload1.HasFile)
{
string fileExtension = Path.GetExtension(FileUpload1.FileName);// Checkout proper file if (fileExtension.ToLower() != ".jpg" && fileExtension.ToLower() != ".jpeg") { lblMessage.Text = "Only File With .jpg and .jpeg Extension are allowed"; lblMessage.ForeColor = System.Drawing.Color.Red; } else { //checkout whether file already exist or not if (File.Exists(Server.MapPath("~/Uploaded\_Image/" + FileUpload1.FileName))) { lblMessage.Text = "Image Already Exist...Please Select Another Image"; lblMessage.ForeColor = System.Drawing.Color.Blue; return; } else { // Upload image FileUpload1.SaveAs(Server.MapPath("~/Uploaded\_Image/" + FileUpload1.FileName)); lblMessage.Text = "File Uploaded Successfully"; lblMessage.ForeColor = System.Drawing.Color.Green; } } }
else
{
lblMessage.Text = "Please Select File to be uploaded";
lblMessage.ForeColor = System.Drawing.Color.Red;
} -
Code To upload Your Image to folder
YourFunctionToUploadImage()
{
//Check FileUpload controll has File Or not
if (ImageFileUploadControll.HasFile)
{
string fileExtention = System.IO.Path.GetExtension(ImageFileUploadControll.FileName);
//Check For Valid Extension
if (fileExtention.ToLower() != ".jpg" && fileExtention.ToLower() != ".jpeg" && fileExtention.ToLower() != ".png")
{
lblUploadstatus.Text = "Invalid File Choose .jpg/.jpeg/.png/.gif";
}
else
{
//check if file already Exists in Folder
if (File.Exists(Server.MapPath("~/Folder_Name/" + ImageFileUploadControll.FileName)))
{
lblUploadstatus.Text = "File Already Exists. Rename filename";
}
else
{
//To Save Image To your Specific Location.
//srever.mappath takes us to Folder which containing our application
ImageFileUploadControll.SaveAs(Server.MapPath("~/Folder_Name/" + ImageFileUploadControll.FileName));
lblUploadstatus.Text = "Upload Status : File Uploaded Successfully";
}
}}
else
{
lblUploadstatus.Text = "Please Select File";
}
}Code To Retrive Images form Folder
PageLoadEvent()
{
//use DirectoryInfo to get all imageFile's Information
DirectoryInfo dir = new DirectoryInfo(Server.MapPath("~/Folder_Name/"));
FileInfo[] fi = dir.GetFiles();//Now you just need to create new image control each time and add ImageUrl to it foreach(FileInfo f in fi) { string imageUrl = f.ToString(); Image img = new Image(); img.ImageUrl = "~/Folder\_Name/"+"imageUrl"; }
}
Hope This Help
Pratik M. Bhuva
Hi Bhuva, Thank you very much for your imp info. I am able to upload and get images, but while showing the images it takes long time. I have almost 150 images in the folder which I need to show with in no time. Request you to pls help me in solving this issue Thanks, Siddiqali
-
Hi Bhuva, Thank you very much for your imp info. I am able to upload and get images, but while showing the images it takes long time. I have almost 150 images in the folder which I need to show with in no time. Request you to pls help me in solving this issue Thanks, Siddiqali
You can use ajax update pannel for fast image loading.