Encode-DeCode required for data transfer in HTTP protocol

Many times Encode and Decode of data is required in web application. The name of the problem may call URL Encoding problem.


Suppose I want to select a file from client pc for upload to server. This file contains some special charecter like +, @, %, & etc. Then Http automatically change that charecter, because http understand some special meaning of that charecters. So when I receive that file from server, files actual name is changed. So this is the main problem.

How to Solve:

You need to encode those files name from javascript and send it to server. Server automatically decode those. If you send data from server then you need to decode those from client(javascript). Bellow I write javascript methods for Encode, Decode special data. We can use those method to solve those problem.

You may thought javasript escape(), deescape() function solve that. Javascript has escape(), deescape() function for encode decode those. But you need to some extra work on those methods, make your code wrox-solid.

You may thought using ajax you transport those data as post request and fixed those. But post request can't solve that.

Javascript Encode Method

function encode(str)
str = escape(str);

str = str.replace('+', '%2B');

str = str.replace('%20', '+');

str = str.replace('*', '%2A');

str = str.replace('/', '%2F');

str = str.replace('@', '%40');

return str;

Javascript Decode Method

function decode(str)

str = str.replace('+', ' ');

str = unescape(str);

return str;