Create Web Service to read images from folder and return path, DateTime of image created in Json format using C# in ASP.Net

eshant.kapoor.5
 
on Nov 04, 2016 01:51 AM
Sample_176646.zip
3360 Views
I want to create webservice which read all image files of folder and return path and datetime of image file in json format.
Download FREE API for Word, Excel and PDF in ASP.Net: Download
AnandM
 
on Nov 04, 2016 03:49 AM
on Nov 14, 2016 06:42 AM

Hi eshant.Kapoor,

I have created a sample which full fill your requirement

HTML

<div>
    <asp:Button ID="btnRetrieve" Text="Retrieve" runat="server" OnClick="btnRetrieve_Click" />
    <br />
    <asp:GridView ID="gvDetails" runat="server" />
</div>

C#

using System;
using System.Data;
using Newtonsoft.Json;

public partial class _Default : System.Web.UI.Page
{
    protected void btnRetrieve_Click(object sender, EventArgs e)
    {
        localhost.ImageDetails imageDetails = new localhost.ImageDetails();
        DataSet dataSet = JsonConvert.DeserializeObject<DataSet>(imageDetails.GetImagePath().ToString());
        gvDetails.DataSource = dataSet.Tables[0];
        gvDetails.DataBind();
    }
}

ImageDetails.cs

using System.Web.Services;
using System.Data;
using System.IO;
using Newtonsoft.Json;
using System.Web.Script.Services;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class ImageDetails : System.Web.Services.WebService
{
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string GetImagePath()
    {
        DataSet ds = new DataSet("ImageFiles");
        DataTable dt = new DataTable("Details");
        dt.Columns.AddRange(new DataColumn[3] { new DataColumn("ImageFilePath"), new DataColumn("ImageName"), new DataColumn("DateTime") });
        string[] directoryForSearch = new string[] { "C", "D", "E" }; //Drives Name.
        for (int k = 0; k < directoryForSearch.Length; k++)
        {
            DirectoryInfo drive = new DirectoryInfo(directoryForSearch[k] + ":\\");
            DirectoryInfo[] folders = drive.GetDirectories("*.*");
            for (int i = 0; i < folders.Length; i++)
            {
                FileInfo[] allFiles = folders[i].GetFiles("*.*");

                if (allFiles.Length > 0)
                {
                    for (int j = 0; j < allFiles.Length; j++)
                    {
                        if (allFiles[j].Extension == ".jpg" || allFiles[j].Extension == ".png" || allFiles[j].Extension == ".bmp" || allFiles[j].Extension == ".gif")
                        {
                            dt.Rows.Add(allFiles[j].DirectoryName, allFiles[j].Name, allFiles[j].LastWriteTime);
                        }
                    }
                }
            }
        }
        ds.Tables.Add(dt);
        return JsonConvert.SerializeObject(ds);
    }
}

ScreenShot