How can I read a file from remote machine?
how can I read the text in t开发者_如何学JAVAhe file from remote machine(from IIS6.0 Virtual directory) either using Javascript or Ajax and copy into client machine 'TEMP/specified' Folder.
Thanks Sudhakar
You can't use JS to read a file from a remote machine because of the same domain policy. You have to do an Ajax request to the same server the page is opened from, the server must fetch the file from the other server with PHP or whatever backend language you're using - anything that doesn't have a same domain policy - and then answer the Ajax call with the contents of the file you fetched.
edit: Of course writing the file on the client machine is impossible too, and there's no solution for that
setting up a ftp server (or ssh daemon) on the client computer could help. then do a JS request to asp.net / PHP script to launch ftp copy (or ssh copy) process to the client server.
You could acheive this using Javascript/Ajax in conjunction with a server-side language, but we have to make a few assumptions before we start:-
- The process will be triggered by a user's web browser
- You have a web server on both source and target servers (remote and local)
- The source server is a "remote machine(from IIS6.0 Virtual directory)"
- The target server is the user's computer
- The user is known and has the target web service setup on their computer
First you might setup a web service on the source server (using ASP.NET). This service would take a request from Ajax and return the contents of the relevant file.
Second you might setup a web service on the target server (using ASP.NET). This service would accept a request from Ajax and save the contents of the request into a Temp folder.
Finally you should create some Ajax on a web page to contact your source server and retrieve the data you need into a local Javascript object (JSON works well here). Once retrieved, you can repackage the data and send it with an Ajax request to your local/target server. This process would be triggered by the user visiting the webpage containing the Ajax.
In summary, you could do this with Javascript/Ajax/ASP.NET but it's not ideal if you don't know who your users are going to be, as they will need to have a local web service setup to save files to their local file system.
The reason you can't use Javascript to write directly to a user's file system is because Javascript is sandboxed when executed inside a web browser.
精彩评论