1

I am facing issue while downloading/exporting XML file from C# model to local machine of browser(I have front end for it). However I am able to download/export the file from C# model to XML and save it on directory on server.

I am using below code for it :

var gradeExportDto =  Mapper.Map<GradeExportDto>(responseGradeDto);
System.Xml.Serialization.XmlSerializer writer = new System.Xml.Serialization.XmlSerializer(gradeExportDto.GetType());
var path = _configuration.GetValue<string>(AppConstants.IMPORT_EXPORT_LOCAL_URL) + "\\"+ responseGradeDto.Code+"_"+DateTime.UtcNow.ToString("yyyy-MM-dd") + ".xml";
System.IO.FileStream file = System.IO.File.Create(path);
writer.Serialize(file, gradeExportDto);
file.Close();

Angular Code :

onExport(selectedData: any): void{
    this.apiService.post(environment.api_url_master, 'ImportExport/ExportGrade/', selectedData).subscribe(result => {
      this.translateService.get('GradeExportSuccess').subscribe(value => this.toastr.success(value));
    }, err => {
      this.toastr.error(err.message);
    });
  }

I need help in getting this file downloaded to local system on which browser is running. Please let me know if more information is required from my side. NOTE : I am not trying to download existing file. I have model in C# which I need to convert in XML and then download it to my local. However I am able to convert it to XML but not able to download on local.

Shardul
  • 309
  • 1
  • 3
  • 17
  • 1
    Possible duplicate of [How to download a file in ASP.NET Core](https://stackoverflow.com/questions/45727856/how-to-download-a-file-in-asp-net-core) – croxy Mar 11 '19 at 12:52
  • @croxy In that solution file is being read from some location. In my case I don't have any file to be read from some where. I have a model as c# class which I am converting to XML format. I am also able to save that XML as .xml file on server. My problem is I want to save it on my local instead of server. – Shardul Mar 11 '19 at 13:01

2 Answers2

1

You cannot save anything directly to a client machine. All you can do is provide the file as a response to a request, which will then generally prompt a download dialog on the client, allowing them to choose to save it somewhere on their local machine.

What @croxy linked you to is how to return such a response. If the issue is that the answer is using an existing file, you can disregard that part. The idea is that you're returning a byte[] or stream, regardless of where that's actually coming from. If you're creating the XML in memory, then you can simply do something like:

return File(memoryStream.ToArray(), "application/xml", "file.xml");
Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
  • How I can I get download prompt? Currently I am not showing any prompt. I have configured path in appsettings.json, so the is getting saved at that location. – Shardul Mar 11 '19 at 14:50
0

Instead of serializing your data into a file, serialize it into a stream eg. MemoryStream and return a File() from your action:

public IActionResult GetXml()
{
    var gradeExportDto =  Mapper.Map<GradeExportDto>(responseGradeDto);
    var writer = new System.Xml.Serialization.XmlSerializer(gradeExportDto.GetType());
    var stream = new MemoryStream();
    writer.Serialize(stream, gradeExportDto);

    var fileName = responseGradeDto.Code + "_" + DateTime.UtcNow.ToString("yyyy-MM-dd") + ".xml";

    return File(stream.ToArray(), "application/xml", fileName);
}
croxy
  • 4,082
  • 9
  • 28
  • 46
  • I am getting below error in front end. error: SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse My frontend is in angular 6. – Shardul Mar 11 '19 at 13:31
  • Yes. My download works but it is saving file to server. – Shardul Mar 11 '19 at 13:57
  • @Shardul Does the download prompt open if you call the above API method? As Chris Pratt already stated, you cannot directly save a file to your clients machine. It should open a download prompt where the user can choose the *save* option as well as the path by himself. – croxy Mar 11 '19 at 14:12
  • @Shardul The error you stated above sounds like you download the file in angular and try to parse it to json. – croxy Mar 11 '19 at 14:12
  • No, download prompt is not there. File is directly getting saved. – Shardul Mar 11 '19 at 14:49
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/189822/discussion-between-shardul-and-croxy). – Shardul Mar 11 '19 at 14:51