-2

I'm creating and saving an xml file using the following code:

File.WriteAllText("D:\\55_SEPA_30-04-2017.xml", strXMLGenerate);

The problem is that the created file saving on server folder not in local drive. How can I save the file anywhere on local drive path. Is there any way to save the file automatically in local "Download" folder? Any help will be highly appreciated.

Edit: I'm using File.WriteAllText for creating and saving file for an Ajax call. In a Ajax calling/ static method the Response.Write/ Response.End() is not supported. So my question is nowhere duplicate with the mentioned question. Please take a look on it.

Thanks

Gopal Biswas
  • 409
  • 1
  • 7
  • 34
  • 1
    Possible duplicate of [C# Asp.net write file to client](https://stackoverflow.com/questions/1072814/c-sharp-asp-net-write-file-to-client) – Naveen Gogineni May 30 '17 at 05:43
  • It is not duplicate with the mentioned question @ Naveen Gogineni – Gopal Biswas May 30 '17 at 06:04
  • Usually people don't want to save the xml to a file. The normal solution is to use a memorystream so you do not need to save to file. – jdweng May 30 '17 at 06:47
  • I need to to save the xml file to my local drive @jdweng – Gopal Biswas May 30 '17 at 06:53
  • Be careful of which folder you save data in. Download folder are only accessible to current user. If file is need by multiple users than pick a folder that all user have credentials to read. – jdweng May 30 '17 at 07:00
  • You cannot save files on local drive using `File.WriteAllText()` or any other C# function. This could be made with some JavaScript as well. Would you care for a such approach? – Tasos K. May 30 '17 at 07:00
  • 1
    From the looks of it, I would say you are trying to save a file to the Client machine using server side code. That is not possible. The only way to do is sending file as a response and letting the browser download it for you. – brainless coder May 30 '17 at 07:05
  • Correct. Could you please tell me the process to do it? @ brainless coder – Gopal Biswas May 30 '17 at 07:12

1 Answers1

1

You can't download file through Ajax. Because of security concerns, JavaScript cannot save files directly to a user's computer.

Kiran Beladiya
  • 441
  • 1
  • 5
  • 11
  • So what is the way to save the file in a static method? – Gopal Biswas May 30 '17 at 07:06
  • It looks like you are using Webmethod with static function. You can't serve file from static method because Response object is not available in static function. The only way is to send file as a response and let the browser download it. – Kiran Beladiya May 30 '17 at 07:12
  • Could you mention the C# code to "send file as a response"? – Gopal Biswas May 30 '17 at 07:15
  • 1
    Please check the answer given here: https://stackoverflow.com/questions/1913027/how-can-you-force-the-browser-to-download-an-xml-file – Kiran Beladiya May 30 '17 at 07:17