thefourtheye's weblog

opinions are my own; try code/suggestions at your own risk

File Upload With jQuery and Ajax

| Comments

The requirement is simple. I should be able to upload files to the server with jQuery and ajax. Lets get started.
<html>
<form>
File Description:<input type="text" id="desc" />
Choose File:<input type="file" id="chosenFile" />
<input type="button" id="submitFile" value="submitTheFile" />
</form>
</html>
Now, the real jQuery stuff
<script type="text/javascript">
jQuery.noConflict();
jQuery(document).ready(function() {
jQuery("#submitFile").click(function() {
jQuery.ajax({
url: "[url to be submitted to]",
type: "POST",
contentType: false,
processData: false,
data: function() {
var data = new FormData();
data.append("fileDescription", jQuery("#desc").val());
data.append("chosenFile", jQuery("#chosenFile").get(0).files[0]);
return data;
// Or simply return new FormData(jQuery("form")[0]);
}(),
error: function(_, textStatus, errorThrown) {
alert("Error");
console.log(textStatus, errorThrown);
},
success: function(response, textStatus) {
alert("Success");
console.log(response, textStatus);
}
});
});
});
<script>
Important things to be noted here are
contentType: false,
processData: false,

contentType will be determined automatically, so we don't have to set that explicitly and processData has to be false, otherwise the data will be processed and transformed into a query string, fitting to the default content-type "application/x-www-form-urlencoded". Next important thing is

data: function() {
var data = new FormData();
data.append("fileDescription", jQuery("#desc").val());
data.append("chosenFile", jQuery("#chosenFile").get(0).files[0]);
return data;
// Or simply return new FormData(jQuery("form")[0]);
}(),
You can read about FormData here. We basically set the values being submitted. The first parameter is the key and the second parameter is the actual value to be passed. We can get the value of any form field with
jQuery("#desc").val()
expect the files. If we use the same for files, we ll get just the file name instead of the file contents. So, we have to do something like
jQuery("#chosenFile").get(0).files[0]
If we dont want to set individual values and want to pass all the fields in the form, we can simply do
data: new FormData(jQuery("form")[0])
Thats it. Enjoy Ajaxified file upload :)
References:

Installing the thefourtheyeEditor - Topcoder Plugin

| Comments

thefourtheyeEditor is a very lightweight plugin for Topcoder Arena to participate in Single Round Matches, which can build testcases and lets the users to store the solutions as local files, so that any editor or IDE can be used to edit them. It also maintains the solutions in the directories named as the SRM's display name.

Features

  1. Very lightweight - Only one jar file. It doesn't depend on any other external jar files.
  2. Organized solutions storage - Solutions will be stored as per the SRM names
  3. File based configuration - Configurations are done in contestapplet.conf file. No need to use UI.

Installation

  1. Download thefourtheyeEditor plugin (thefourtheyeEditor.jar) from https://github.com/thefourtheye/thefourtheyeEditor/releases/download/latest/thefourtheyeEditor.jar
  2. Open topcoder contest applet and login with your username and password

  3. Select Editor from the Options menu. You 'll see something like this

  4. Click on Add and you 'll get a window like this. Fill in the details as you see in this picture. Actually you can give any name in the Namefield and in ClassPath field, you have to locate the thefourtheyeEditor.jar file using Browse button. EntryPoint must be exactly the same as thefourtheyeEditor.Main.

  5. Once these steps are done, the Editor preferences page will look like this

  6. Click on Save button and close that window. That's it. Installation is complete :)

Ubuntu Bug Related to Network and Power

| Comments

Last week, I faced this weird problem. When my laptop is not connected to a power source (not on battery), I could not connect to LAN network with my LAN cable, but Wi-Fi worked fine. I struggled a lot for a week and then I found a solution in the internet.

All you have to do is to execute this one liner in your terminal.

echo on > /sys/class/net/eth1/device/power/control
Here eth1 corresponds to my second ethernet interface. It might vary from machine to machine. And if the directories eth, device and power dont exist, you might have to manually create them.

Compiling Node.js Scripts in Windows 7 With Sublime Text 3

| Comments

This is a continuation of Compiling CPP 11 Programs with Sublime Text 3 in Ubuntu where we saw how to configure Sublime Text 3 in Ubuntu 13.04 to compile C++ 11 programs. In this post, we ll see how to execute Node.js programs in Windows 7 machine's Sublime Text 3. I am going to assume that Node.js is installed properly and PATH variable is also set properly. If you are using Windows Installer, we dont have to worry about this.

  1. We need to create the following directory structure in the User's home directory AppData\Roaming\Sublime Text 3\Packages\JS\. In my machine, home directory is C:\Users\[username]. To know the current user's home directory, open Cmd.exe and type echo %userprofile%.
  2. In that directory, create a file called "JS.sublime-build". So, the location of the file from the home directory is AppData\Roaming\Sublime Text 3\Packages\JS\JS.sublime-build You can name the sublime-build file as anything you want. I have simply named it here as JS.
  3. Copy and paste the following text in to it.
    {
    "cmd": ["node.exe", "${file}"],
    "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
    "working_dir": "${file_path}",
    "selector": "source.js",
    "variants":
    [
    {
    "name": "Run",
    "cmd":["node.exe", "${file}"]
    }
    ]
    }
  4. Thats it. Open Sublime Text 3. Click on Tools->Build System. You should see JS as one of the options. From now on, you can execute node.js scripts simply by pressing Ctrl-B.

Compiling C++11 Programs With Sublime Text 3

| Comments

For Windows 7, you may want to read this post http://www.thefourtheye.in/2013/07/Compiling-Node.js-scripts-in-Windows-7-with-Sublime-Text-3.html

Today I installed Sublime Text 3's public beta on my Ubuntu 13 and the first thing I noticed is, the inability to compile C++ 11 programs. The obvious problem is, it was missing -std=c++0x parameter to g++. I tried to figure out how to edit the build parameters of Sublime. After an hour's struggle managed to figure out.

  1. You need to create the following file ~/.config/sublime-text-3/Packages/C++/C++.sublime-build. The ~ refers to the home directory of the current user.
  2. Now insert the below seen text to that file
    {
    "cmd": ["g++", "-std=c++0x", "${file}", "-o", "${file_path}/${file_base_name}"],
    "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
    "working_dir": "${file_path}",
    "selector": "source.c, source.c++",
    "variants":
    [
    {
    "name": "Run",
    "cmd":["bash", "-c", "g++ -std=c++0x '${file}' -o '${file_path}/${file_base_name}' && '${file_path}/${file_base_name}'"]
    }
    ]
    }
  3. Save this file and close it. Sublime will pick up the changes immediately.
This will take care of compiling C++ 11 programs.