Wednesday, April 25, 2012

Checking what are the active queries on the SQL Server

SELECT sqltext.TEXT,
req.session_id,
req.status,
req.command,
req.cpu_time,
req.total_elapsed_time
FROM sys.dm_exec_requests req
CROSS APPLY sys.dm_exec_sql_text(sql_handleAS sqltext

Friday, June 24, 2011

FancyUploader + Classic ASP.NET

Good day people! Last few days I have been torturing my head how to make an upload control like FancyUpload work with ASP.NET and now I can harvest the glory!

This is the simpliest way I can describe how to make it work so some steps may seem a bit ... let's say noobish, but I want to make sure that even those who are not very familiar with the programming to be able to do it.

1. Go to the FancyUpload page and download the latest version of the code. Simple enough huh?
2. Unzip the files into the directory of the web app.
3. Open the file Build.html and start copying the code from there to the desired aspx page. I think most people will be able to differ what must they copy and what not...
4. In the copied text edit the links to the different stylesheets and scripts.
Most importantly to make it work edit

window.addEvent('domready', function() { // wait for the content

// our uploader instance
var up = new FancyUpload2($('demo-status'), $('demo-list'), { // options object
// we console.log infos, remove that in production!!
verbose: true,
// url is read from the form, so you just have to change one place
url: $("form-demo").action

the original url is $("form-demo").action so you have to change it to the relative link of the handler you will create in the next few steps. I am saying 'have to', because Firefox and Chrome have problem getting the link from the form action attribute.
5. Create a Handler (*.ashx) with name as you have writen in step 4.
3. Here is a code sample of what the handler should look like (some of the things written here are not required like the namespace for example):

<%@ WebHandler Language="C#" Class="JQueryUpload.FileUpload" %>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;

namespace JQueryUpload
{
///
/// File Upload httphandler to receive files and save them to the server.
///
public class FileUpload : IHttpHandler
{

public void ProcessRequest(HttpContext context)
{
//checks if there are given any files to upload
if (context.Request.Files.Count == 0)
{

context.Response.ContentType = "text/plain";
context.Response.Write("No files received.");

}
else
{
HttpFileCollection files = context.Request.Files;
for (int i = 0; i < files.Count; i++)
{
HttpPostedFile uploadedfile = context.Request.Files[i];

string FileName = uploadedfile.FileName;
string FileType = uploadedfile.ContentType;
int FileSize = uploadedfile.ContentLength;
//don't forget you must give permissions to the Network Service to write inside the folder
string filePath = HttpContext.Current.Server.MapPath("~/UploadedFiles/");
uploadedfile.SaveAs(filePath);
context.Response.ContentType = "text/plain";
context.Response.Write("{\"name\":\"" + FileName + "\",\"type\":\"" + FileType + "\",\"size\":\"" + FileSize + "\"}");
}
}

}

public bool IsReusable
{
get
{
return false;
}
}

}
}



Now if it doesn't work you should see if you haven't forgotten some

Special thanks to the people who contributed for this: Harald Kirschner(creator of FancyUpload),Ben Newton(Article about FancyUpload + classic ASP)

Tuesday, January 11, 2011

Live conversion of video files to .flv

Lately I have been interested into how exactly http://youtube.com does all the conversion of different kinds of videos into flv.
Without much of a searching I have found a way to convert it and it's quite easy.

You need 3 items to begin with:

these are a complete solution to record, covert and stream audio and video.

Now is time for the webpage itself.
1. Go into Internet Information Service Manager and create a new .net web application.
2. Copy the 3 files above into the created web application directory.
3. In the application directory create 2 folders: "OriginalVideo" and "ConvertVideo"
4. Give those folders security permissions so that we can upload to them
5. Create a new page. Name it however you desire it doesn't matter.
6. Put on 2 controls on it. 1 Button with ID="btnSubmit" OnClick="btnSubmit_Click"
and a FileUpload Control with ID="fileuploadVideo"
7. Copy the code below into the code behind

The code that I am posting is in C#

private bool ReturnVideo(string fileName)

{

string html = string.Empty;

//rename if file already exists

int j = 0;
string AppPath;
string inputPath;
string outputPath;
string imgpath;
AppPath = Request.PhysicalApplicationPath;
//Get the application path
inputPath = AppPath + "OriginalVideo";
//Path of the original file
outputPath = AppPath + "ConvertVideo";
//Path of the converted file
imgpath = AppPath + "Thumbs";
//Path of the preview file
string filepath = Server.MapPath("~/OriginalVideo/" + fileName);
while (File.Exists(filepath))

{

j = j + 1;

int dotPos = fileName.LastIndexOf(".");

string namewithoutext = fileName.Substring(0, dotPos);

string ext = fileName.Substring(dotPos + 1);

fileName = namewithoutext + j + "." + ext;

filepath = Server.MapPath("~/OriginalVideo/" + fileName);

}

try

{

this.fileuploadVideo.SaveAs(filepath);

}

catch

{

return false;

}

string outPutFile;

outPutFile = "~/OriginalVideo/" + fileName;

int i = this.fileuploadVideo.PostedFile.ContentLength;

System.IO.FileInfo a = new System.IO.FileInfo(Server.MapPath(outPutFile));

while (a.Exists == false)

{



}

long b = a.Length;

while (i != b)

{



}

string cmd = " -i \"" + inputPath + "\\" + fileName + "\" \"" + outputPath + "\\" + fileName.Remove(fileName.IndexOf(".")) + ".flv" + "\"";

ConvertNow(cmd);

string imgargs = " -i \"" + outputPath + "\\" + fileName.Remove(fileName.IndexOf(".")) + ".flv" + "\" -f image2 -ss 1 -vframes 1 -s 280x200 -an \"" + imgpath + "\\" + fileName.Remove(fileName.IndexOf(".")) + ".jpg" + "\"";

ConvertNow(imgargs);

return true;

}

private void ConvertNow(string cmd)

{

string exepath;

string AppPath = Request.PhysicalApplicationPath;

//Get the application path

exepath = AppPath + "ffmpeg.exe";

System.Diagnostics.Process proc = new System.Diagnostics.Process();

proc.StartInfo.FileName = exepath;

//Path of exe that will be executed, only for "filebuffer" it will be "flvtool2.exe"

proc.StartInfo.Arguments = cmd;

//The command which will be executed

proc.StartInfo.UseShellExecute = false;

proc.StartInfo.CreateNoWindow = true;

proc.StartInfo.RedirectStandardOutput = false;

proc.Start();



while (proc.HasExited == false)
{}

}

protected void btnSubmit_Click(object sender, EventArgs e)
{
ReturnVideo(this.fileuploadVideo.FileName.ToString());
}

And there you go! A simple conversion.

Big thanks to Bahadur Pur Rajoa for the help in this solution.

Monday, January 4, 2010

PHP connecting to MySQL

To open a connection to MySQL DB with PHP use the following:


$user = 'admin';
$pass = 'password';

$connection = mysql_connect($host, $user, $pass) or die ('Error connecting to mysql');

$dbname = 'tech';
mysql_select_db($dbname);
?>

Here $host is the name of MySQL server. If the webserver is on the same machine with the database server the default values can be localhost or 127.0.0.1 . $user and $pass are the valid MySQL profile name and password.

If you want for your database to work you need to select it using the mysql_select_db() function after connecting to MySQL otherwise the queries won't work.

The webhost sometimes requires the port to be specified, so if you must enter that the needed port is 8080, then you must modify the $host = 'hostname.com:8080';

Friday, December 18, 2009

Installing PHP on Windows 7


First of all, you need PHP (obviously). Go to the PHP download page and grab the latest non-thread-safe ZIP archive for Windows. As of writing this, the package is called "PHP 5.2.8 Non-thread-safe zip package". Unzip the archive to a folder on your hard disk (I use C:\php5) and create a copy of php.ini-recommended called php.ini. There, add the following configuration setting:

cgi.force_redirect = 0

Now let's install IIS. "Business" version of Windows 7 is the least needed to have for installing. Go to
Start/Control Panel/Programs/Turn Windows Features on or off and check on the Internet Information Services entry. Activate the World Wide Web Services/Application Development Features/CGI node and also Web Management Tools/IIS Management Console.

Start the IIS Management Console; Open up the start menu, enter inetmgr and hit Enter. There, go to the Sites/Default Web Site/Handler Mappings node and double-click on the "Handler Mappings" entry.

The Actions panel on the right hand side changes. Now you can see an option called Add Module Mapping. Clicking on it opens up a dialog which you fill out as you can see in the following figure.


If you don't see the FastCgiModule entry, you most likely forgot to check the CGI node when installing IIS. Otherwise, close the Add Module Mapping dialog by clicking on OK. Now confirm that you want to create a FastCGI application; click Yes.