Friday, June 20, 2025

Website admin tool - access rules for files and folders

Access rules are store in web.config for each folder

example like below:-

<?xml version="1.0" encoding="utf-8"?>

<configuration>

    <system.web>

        <authorization>

            <allow roles="Administrator" />

          <deny users="*" />

        </authorization>

    </system.web>

</configuration>

How to download the file using handler in asp.net c# of any format or extension

  public void ProcessRequest(HttpContext context)

  {

    if (context.User.Identity.IsAuthenticated)

    {

      string filename = context.Request.QueryString["File"];

      //Validate the file name and make sure it is one that the user may access

      context.Response.Buffer = true;

      context.Response.Clear();

      context.Response.AddHeader("content-disposition", "attachment; filename=" + filename);

      context.Response.ContentType = "application/octet-stream";

//File(HelloWorld.txt) should be present on server in App_Data folder , otherwise it will show error

      context.Response.WriteFile("~/App_Data/" + filename);

    }

  }

aspx page

<a href="MyFileHandler.ashx?File=HelloWorld.txt">Click Here to Get File</a>

How to download the file using transmitfile function in asp.net c#

 Response.ContentType = "application/pdf";

Response.AppendHeader("Content-Disposition", "attachment; filename=MyFile.pdf");

//MyFile.pdf should be present in Files folder on server. other wise it will show error file not found.

Response.TransmitFile(Server.MapPath("~/Files/MyFile.pdf"));

Response.End();


reference:-https://www.c-sharpcorner.com/UploadFile/afenster/how-to-download-a-file-in-Asp-Net/

why Http Handler are used - Introduction to handler

 Http handlers are used for


Binary files

Dynamic image views

Performance-critical web pages

XML files

Minimal web pages


and these are fast and it only gives minimum response. example when you write  

Response.ContentType = "text/plain";

         //MediaTypeNames.Text.Plain

in aspx page it will give lot of information.

and when you write this in handler it will give only specific information that you want.


Also no return type of processrequest funtion in http handler

you can create other return type function in handler and also create model in handler.

Thursday, June 19, 2025

Computer vs television - difference between computer and television

 Computer is having data and english hindi lanuguage all time. and text is very small


but in television we have visual things , and text is very large if it had

Tuesday, June 17, 2025

How to split the comma separated string to list of array in C#

string tags = "9,3,12,43,2"


 tags.Split(',').Select(int.Parse).ToList();

How to convert bytes to string and string to bytes in c#

 


        string author = "Mahesh Chand";

        // Convert a C# string to a byte array

        byte[] bytes = Encoding.ASCII.GetBytes(author);

        foreach (byte b in bytes)

        {

            Response.Write(b );

            Response.Write(Environment.NewLine);                       

        }


bytes to string

string myString = Encoding.ASCII.GetString(bytes);

Response.Write(str);

How to convert bytes to base64string in c#

using fileupload control

 Convert.ToBase64String(fileUpload1.FileBytes);


using memory stream

 Stream fs = fileUpload1.PostedFile.InputStream;

        BinaryReader br = new BinaryReader(fs);

        

        Byte[] bytes = br.ReadBytes((int)fs.Length);

 Convert.ToBase64String(bytes );



base64 string meaning

the upper- and lower-case Roman alphabet characters (A–Z, a–z), the numerals (0–9), and the "+" and "/" symbols, with the "=" symbol as a special suffix code



How to save bytes data to image

using memory stream

MemoryStream m = new MemoryStream(bytes); 

System.Drawing.Image MyImage = System.Drawing.Image.FromStream(m);

MyImage.Save(filepathandname)

====================

using fileupload control

fileUpload1.FileBytes;

string filepathandname = Server.MapPath(string.Format("~/uploads/{0}-{1}.{2}", DateTime.Now.ToShortDateString(), Guid.NewGuid(), ".png"));

MyImage.Save(filepathandname)

How to save a file to binary data to varbinary sql server in asp.net c#

Using fileupload field

byte[] bb = fileUpload1.FileBytes;

  SqlConnection con = new SqlConnection(ConnectionString.ConString);


            con.Open();

            SqlCommand cmd = new SqlCommand("INSERT INTO UploadFiles (FileData, ContentType) VALUES (@filedata, @ContentType)", con);

            cmd.CommandType = System.Data.CommandType.Text;

            

            

            cmd.Parameters.AddWithValue("@FileData", fileUpload1.FileBytes);

            cmd.Parameters.AddWithValue("@ContentType", fileUpload1.PostedFile.ContentType);


            cmd.ExecuteNonQuery();

            con.Close();

===============================

using binary reader

 Stream fs = fileUpload1.PostedFile.InputStream;

        BinaryReader br = new BinaryReader(fs);

        

        Byte[] bytes = br.ReadBytes((int)fs.Length);

  Stream fs = fileUpload1.PostedFile.InputStream;

 Byte[] bytes = br.ReadBytes((int)fs.Length);

object vs instance - object instance - instance vs object in c#

 object are also called instance.

Friday, June 6, 2025

Fundamental unit of data

 Fundamental unit of data in computer is bit or binary digit  0, 1

how to convert byte array to image in asp.net

Byte[] bytes =  fileUpload1.FileBytes;

  MemoryStream m = new MemoryStream(bytes);

     System.Drawing.Image MyImage = System.Drawing.Image.FromStream(m);            

            string filepathandname = Server.MapPath(string.Format("~/uploads/{0}-{1}-{2}", DateTime.Now.ToShortDateString(), Guid.NewGuid(), ".png"));

            MyImage.Save(filepathandname);

Thursday, June 5, 2025

How to see or view a base 64 image file or image file in HTML page and how to see image file base 64 with URL

 <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABkAAAAOECAYAAAD5Tf2iAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAP+lSURBVHhe7N0LYFTVuTb+B1AkeIGEm0CABBAiglyD3CINoqJoKRSRNqWtSEUK51NrgVKPHD48LYLHqt8fSvEgtlJaRAqlFouKIAUBCYIXREAggVwAA0lQgQgK//2uvfbMnvueaybx+dld9m1m1r5OZr17rbdOt249LoGIiIiIiIiIiIiIiKgWqav/JSIiIiIiIiIiIiIiqjUYACEiIiIiIiIiIiIiolqHARAiIiIiIiIiIiIiIqp1GAAhIiIiIiIiIiIiIqJahwEQIiIiIiIiIiIiIiKqdep069bjkh4nIiIiIiIiIiKq1erXv0KPEVFtdf78V3qMvu3YAoSIiIiIiIiIiIiIiGodBkCIiIiIiIiIiIiIiKjWqdeixbWz9Hi1qFevHn7zmydwyy1DUFVVhaKiIr2EiIiIiIiIiIgoturVu0yP

Sunday, June 1, 2025

Web api get post delete and put difference in apicontroller class

 public class ValuesController : ApiController

{


  //For get all data

    public IEnumerable<BookMaster> Get()

    {

           }

    

   

//For get by ID

    public void Get(int id)

    {

        }

        


    [HttpPost] // insert or add or create new

    public string Post([FromBody]string value)

    {

        return value;

    }


    // PUT api/<controller>/5    update

        public void Put(BookMaster bookMaster) 

    {    

    }


    // DELETE api/<controller>/5

    [HttpDelete]

    public void Delete(int id)

    {

    }


How to use ObjectDataSource in asp.net with Model or business object and controller or login layer

 >Add object datasource from controls in visual studio

> Add TypeName Property(class name .cs file) where you have get, update and delete methods.

>Click on configure data source and select dataset or typename file and you will get select insert update delete methods and click on finish and use this object datasource id in grid view or other controls. 

bulk insert with single insert query in sql

 INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);

Transaction in asp.net c#

 https://learn.microsoft.com/en-us/aspnet/web-forms/overview/data-access/working-with-batched-data/wrapping-database-modifications-within-a-transaction-cs