سؤال

Ok I know this is a probably a very basic question but I'm more of a winform person.

Question is simple.

If we Add System.IO to a Web Form . Can we then use a StreamWriter to write a log file to the end user Computer?

For example if page_Load use the following code:

StreamWriter sr=New StreamWriter("C:\abc.log)

sr.Write("ABC")

Then where this abc.log file is created? on webserver or on end user? If on websever then how can we write it on the end user machine?

I want to write a log file on the client machine. What are options? Is using a cookie an option?

هل كانت مفيدة؟

المحلول 3

As far my knowledge you can't write the client file using asp.net.
In your example you are writing the file where application is hosted.

Means the application server's c:\abc.log

نصائح أخرى

No - that is not the way the web works... You could present the Log as an download, that would be the cleanest solution.

As everyone in this thread has pointed out: this is simply not possible. Now depending on what you are trying to store you could leverage cookies to store some information on the users computer but this can be cumbersome and you are limited to 4K of information. That being said HTML5 offers the new JavaScript objects localStorage and sessionStorage which are basically a key/value dictionaries. localStorage would probably suit your needs best I think since it will persist information on the users PC after the browser is closed. For example:

 function logSomeData(message) {
       localStorage.logFile += message;
 }

 function showData() {
      alert(localStorage.logFile);
 }

If you run the logSomeData function in a browser, close the browser, then come back and run the showData() function the value you added to logFile will be persisted. This isn't a new concept, this is doing exactly what cookies do today except that localStorage is easier to work with (in my opinion) and it can store much more information (2MB to 10MB depending on the browser). It is worth pointing out that this is a new technology so older browsers like IE7 can't use localStorage.

Please let me know if you have any other questions!

All the code behind in asp.net runs on the server so the code you posted will write to the server machine.

Browsers don't allow interaction with the local machine file system for security reasons

You could use some other technology (e.g. an ActiveX control hosted in a web page)

why you want log file on client site ?

if you want to store some information on client site use cookies

If you're stuck to Internet Explorer 6, the only solution to be able to store client-side data, is to use userData Behavior. It well let you store up to 128kb.

I would recommend you to have a look at jStorage. Which is:

a cross-browser key-value store database to store data locally in the browser - jStorage supports all major browsers, both in desktop (yes - even Internet Explorer 6) and in mobile.

It's mainly based on HTML5 localstorage but it will switch to any available technology (like userData Behavior) when HTML5 is not supported.

You could download the text file of log to client machine if you flush the .txt file in the respose object i,e (response.write)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top