29 July, 2013

File Upload to Server with ASP.net

File Upload to Server with ASP.net Uploading file to server from client sometimes very tricky. Sometimes it is required that you will not upload file more than certain size. This is the step by step approach to upload file from client to server.

Step 1: Add html input type as file

<input id ="uploadFile" type="file" runat="server" />

Step 2: Create a button to upload file in the server

<asp:Button ID="Button1" runat="server" OnClick="Button1_Click"  />

Step 3: Create a folder in the web project like “TempFiles

Step 4: Upload file in the server.
    protected void Button1_Click(object sender, EventArgs e)
        if (this.uploadFile.PostedFile != null)
            // Get a reference to PostedFile object
            HttpPostedFile huploadFile = uploadFile.PostedFile;

            // Get size of uploaded file
            int nFileLen = huploadFile.ContentLength;

            decimal fileSize = uploadFile.PostedFile.ContentLength / 1024;
            if (fileSize > 100) //If file size is no more than 100 KB 
                this.lblMsg.Text = "Size of this image is greater than 100KB";

            byte[] scriptData = new byte[nFileLen];

            // Read uploaded file from the Stream
            huploadFile.InputStream.Read(scriptData, 0, nFileLen);

            string filePath = Server.MapPath("TempFiles");

            filePath = filePath + "\\" + Path.GetFileName(uploadFile.PostedFile.FileName); //concate filename with file path
            FileStream newFile = new FileStream(filePath, FileMode.Create);

            // Write data to the file
            newFile.Write(scriptData, 0, scriptData.Length);

            // Close file

            lblMsg.Text = "This is an empty file";


07 July, 2013

Which Service is using which port?

Every day we fell in some difficulties and want to know which service is using by which port? Ports are divided into three ranges: The well-known ports (from 0 to 1023), the registered ports (from 1024 to 49151) and Dynamic (Private) ports (from 49152 to 65535). The Internet Assigned Numbers Authority (IANA) is responsible for the global coordination of the port assignment. Here I will show some tools to figure out the problem. Two well-known utility NETSTAT.EXE and TASKLIST.EXE will be used here.

TASKLIST.EXE is used to know the list of processes with its owing id. When we run >tasklist in command window we see the following output.

Suppose, we want to know which port is used by skype? After running >>tasklist you will see the above output. Here you will see Image Name: Skype.exe with process id or PID: 3224. Now if you want to know which process owes process id or PID 3224? Just run >>tasklist /FI “PID eq 3224”. You will see the following output.

NETSTAT.EXE is used to identify what ports are being used by a particular process. The syntax that we will be using for NETSTAT.EXE is as follows: netstat.exe –a –n –o. Here,
  • -a: listing of all connections and listening ports
  • -n: display address and port numbers in numerical form
  • -o: display the owning PID associated with each connection
Now after running >netstat –a –n –o , you will see the following screen.

If you look at the output, you will see that local address of PID 3224 is which mean process id 3224 used port 55956. You already know that 3224 is the PID of process Skype.exe. So, you came to know that process skype.exe is used port 55956.