Total Pageviews

Monday 5 December 2011

How to Java Application in Eclispe


  •  Download the JDK jdk-6u20-windows-i586.exe and Setup on your machine.
  • Create a Environment Variable name JAVA_HOME value C:\Program Files (x86)\Java\jdk1.6.0_20 

  • Open  \eclipse\eclipse.exe .
  • Go to File->Import then import your sources and run.



Thursday 1 December 2011

How to prevent downloading when user's allocated size or period exceeds

It is handled using httphandler in ASP.NET. The handler code is bellow ..



public class DownloadHandler : IHttpHandler
{
       public DownloadHandler()
       {
              //
              // TODO: Add constructor logic here
              //
       }

    public bool IsReusable
    {
        get { return true; }
    }

    /// <summary>
    /// Enables processing of HTTP Web requests by a custom HttpHandler that implements the <see cref="T:System.Web.IHttpHandler"/> interface.
    /// </summary>
    /// <param name="context">An <see cref="T:System.Web.HttpContext"/> object that provides references to the intrinsic server objects (for example, Request, Response, Session, and Server) used to service HTTP requests.</param>
    public void ProcessRequest(HttpContext context)
    {
 
        long currentDownloadedAmount = 0;
        try
        {


            // File name
            string fileName = context.Request.Url.Segments[context.Request.Url.Segments.Length - 1]; ;
            fileName = context.Server.UrlDecode(fileName);
            string filePath = @"d:\ATVMovies\" + fileName;

            FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            // Total bytes to read
            long dataToRead = fileStream.Length;
            context.Server.ScriptTimeout = 600;
            context.Response.Buffer = true;
            context.Response.Clear();

            context.Response.ContentType = "application/octet-stream";
            context.Response.AddHeader("Content-Disposition",
                                       "attachment; filename=\"" + filePath + "\";");

            context.Response.AddHeader("Content-Length", dataToRead.ToString());

            int ChunkSize = 1024; //256 MB =256*1024



            // Buffer to read 10K bytes in chunk
            byte[] buffer = new Byte[ChunkSize];
            int offset = 0;
            int length;
            long UserRestAmount = MovieHelper.getMovieBalance((Guid)Membership.GetUser().ProviderUserKey);

            // Read the bytes.
            while (dataToRead > 0)
            {
                // Verify that the client is connected.
                if (context.Response.IsClientConnected)
                {

                    // UserRestAmount = MovieHelper.getMovieBalance((Guid)Membership.GetUser().ProviderUserKey);
                    if (UserRestAmount <= 0)
                    {
                        fileStream.Close();
                        dataToRead = -1; //disconnected
                        break;
                    }
                    // Read the data in buffer
                    length = fileStream.Read(buffer, offset, ChunkSize);


                    //offset += ChunkSize;

                    // Write the data to the current output stream.
                    context.Response.OutputStream.Write(buffer, 0, (int)length);

                    // Flush the data to the HTML output.
                    context.Response.Flush();

                    buffer = new Byte[ChunkSize];
                    dataToRead = dataToRead - length;

                    currentDownloadedAmount += length;
                    UserRestAmount -= length;

                }
                else
                {
                    //prevent infinite loop if user disconnects

                    dataToRead = -1;
                }
            }
            if (dataToRead == 0 || dataToRead == -1)  // complete or disconnected
            {
                MovieHelper.UpdateDownloadSize((Guid)Membership.GetUser().ProviderUserKey, currentDownloadedAmount);
                currentDownloadedAmount = 0;



            }

            fileStream.Close();
        }
        catch (Exception ex)
        {
            MovieHelper.UpdateDownloadSize((Guid)Membership.GetUser().ProviderUserKey, currentDownloadedAmount);
            currentDownloadedAmount = 0;
            context.Response.Write(ex.Message);
        }
     

     
    }


   

  
}