Creating and Deleting Files on Fly on WebServer through Javascript (Magic of HTML and Jquery)
We usually know the methods of POST and GET for posting and fetching data from WebServer.
There are many other HTML verbs used . Two of them are PUT and DELETE
PUT - used for moving,updating creating files through WEBDAV on webserver.
DELETE - used for deleting file through WEBDAV on webserver
WEBDAV - Web distributed Versioning and Authoring originally developer by IEE and accepted by HTML community as a standard for various Webserver functioning.
As though it is not easy to create a request for PUT and DELETE as it contains raw html and do not use forms.
but through JQUERY we can acheive this in a much simple way.
1) jquery has ajax function which helps in creating ajax requests asynchronously
2) we can setup this call this ajax function for creating a file on webserver.
$.ajax( {data:'MyTestData', type:'PUT', url:'http://localhost/TestWeb/TestFile.txt' } )
Here we are setting up ajax request for PUT verb which will create a file TestFile.txt on TestWeb Folder with data
'MyTestData' written into it.
If we want to insert some heavy data into this file we can also use like this
$.get('http://localhost/TestWeb/Default.aspx' , null, function(testdata) {
$.ajax( {data:testdata, type:'PUT', url:'http://localhost/TestWeb/TestFile.txt' } )
}, null);
Here we have requested data for page Default.aspx which will return HTML code for page through ajax.
function(testdata) is function which get called when ajax request has been successfully completed with the testdata as return value from response.
We can then pass this testData to our previous method which will create file testFile.txt on server and insert the data into this file.
Same way we can also do for DELETE
$.ajax( { type:'DELETE', url:'http://localhost/TestWeb/TestFile.txt' } )
Here we dont use data option as we only want to delete the file from server.
So in a way we can create files and delete them on fly just by using simple jquery and knowledge of HTML verbs.
.