Programming

How to force download a file using ASP.NET

There are many cases where we need to let file downloads being processed by an aspx file. Here in this code this code will force a file to be downloaded through an aspx file. let assume your file path  is in “string path” variable. Add this code in your aspx file cs file.

 


string path = "the path to your file";
string fileName = "Add your file name";
string contentType= "Add your file content type. ex: for text file it is 'text/plain'";
string contentLength = "add string presentation of your file size in bytes";

response.Clear();
response.ClearHeaders();
response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
response.AddHeader("Content-Length", contentLength);
response.ContentType = contentType;
FileStream f = new FileStream(path, FileMode.Open);
byte[] buffer = new byte[8 * 1024];
int len;
while ((len = f.Read(buffer, 0, buffer.Length)) > 0)
{
    response.OutputStream.Write(buffer, 0, len);
}
response.OutputStream.Flush();
response.OutputStream.Close();
response.Flush();
response.Close();
response.End();
No Comments

Create custom notification on your Ubuntu desktop using python

You can easily create custom notifications for your applications on ubuntu. Ubuntu’s current notification system is called NotifyOSD. It provides api for several languages. Using python-notify library it is really so easy than in any other.

Ubuntu by default have the python-notify installed. If don’t install it using

sudo apt-get install python-notify
No Comments

Read more

Convert unicode codepoints to unicode hex values in java

In a part of our crawler development, we encountered a Bangla news site (http://www.kalerkantho.com/) which uses code points instead of unicode hex valus in their website. Although it renders banla fonts in browser, but when viewings source it only shows code points, so when downloaded by crawler we only got কકى similar.  For the indexing purpose we needed to convert them to hex values so that it renders bangla font anywhere. The process of converting is really so simple.

1 Comment

Read more

Extract hyperlinks from html using regular expression in java

2 years ago, I worked with an crawler which can fetch webpages from internet, then parse the links from the page and then visit all the pages linked to the page. At that time I didn't have any idea about regular expressions. So I had to write around a 500 hundred line code to parse links and meta tags from html.

Yesterday, I had to do the same job again. This time I took up regular expression to parse the html <a tags followed by href attribute to extract the links.

Regular expressions can be difficult to understand if written at once, so I am going to write it in easy way first, then i ll make it complex to support variations in page links.

No Comments

Read more

Binary to decimal and decimal to binary conversion in C

This program will show you to convert a binary to decimal and vice versa.

This will take a character array input which is the binary value. Then it will convert the binary value to decimal and then convert it again from decimal to binary. Here is the code.

No Comments

Read more

Create a lan messenger using socket programming in java

For my networking course lab project I developed an messenger for lan using java. It uses socket protocol for communication.

1 Comment

Read more

Generate random number within a range in java

If want to generate random numbers within a minimum and maximum limit you can easily do this in java using this function.

public int getRandom(int min, int max)
{
    return (int) (Math.random() * (max - min + 1) ) + min;
}
No Comments

Create a notepad like windows in java

When I was in 3rd semester I had a project to create a notepad in java. I tried to make it look closer to windows notepad as much as possible. It includes 5 java classes.

The main thing is ofcourse the notepad.java class First take a look at the code

4 Comments

Read more

How to create a client server socket connection in python

Creating server socket connection made so much easy in python. You need to create two script. One for server and another for client. In the server script will create a socket in a port number and start listening to that port. And in the client you will need to connet to that port and host ip.

1 Comment

Read more

Reading an web page source using java

I needed to develop an web crawler few days ago. My first challange was to read a website source code using java. Here is the code I wrote to read a webpage. It will read the http response of url given and print it out.

package webcrawler; 

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;  

/**  *  * @author Burhan  */

public class readUrl
{
    StringBuffer content;
    String contentLowerCase;
    String line;
    String type = null;
    URL url = null;
    URLConnection urlConnection;
    InputStream urlStream;
    DataInputStream html;

    public readUrl(String urlStr) throws MalformedURLException, IOException
    {
        url = new URL(urlStr);
        urlConnection = url.openConnection();
        urlStream = url.openStream();
        type = urlConnection.getContentType();
        if(type==null)
            return;
        else if( type.compareTo("text/html") != 0 ) // not allowed type
            return;             // only htmls are here
        content = new StringBuffer();

        html = new DataInputStream(urlStream);

        while ((line = html.readLine()) != null)
        {
            content.append(new StringBuffer(line));
            content.append('\n');
        }
        System.out.print(content);
    }

    public static void main(String args[])
    {
        try
        {
            new readUrl("http://www.dscripts.net");
        }
        catch(MalformedURLException ex)
        {
            ex.printStackTrace();
        }
        catch(IOException ex)
        {
            ex.printStackTrace();
        }
    }
}
No Comments
Page 1 of 3123