How to upload xml file to asp.net using c #

I am using asp.net web application with mvc3 . I am new to mvc3. I have a download button on my web page. when i click the download button i want to open this xml file .

I tried with some code changes in ActionResult , but I did not open the file. Using the code below, I get a download popup. when I am about to open the file, I get some kind of exception, as shown below. Can someone help me do this? Help solve this problem. :-)

Thanks at Advance.

My code in the controller:

public FileResult Download(string id) { string fid = Convert.ToString(id); var model = service.GetAllDefinitions().First(x => x.ID == id); var definitionDetails = new StatisticDefinitionModel(model); var definition = definitionDetails.ToXml; string fileName = definitionDetails.Name + ".xml"; string contentType = "text/xml"; return File(Encoding.Unicode.GetBytes(definition), contentType, fileName); } 

The exception is:

 The XML page cannot be displayed Cannot view XML input using XSL style sheet. Please correct the error and then click the Refresh button, or try again later. -------------------------------------------------------------------------------- A name was started with an invalid character. Error processing resource 'file:///C:/Users/asub/Downloads/fileNamegd... < 
0
source share
2 answers

If you return FileResult, it will be a file; if you return a string, it will open in a browser.

Update : This code will return the file to download

 public FileResult GetXmlFile() { string xml=""; //string presented xml var stream = new MemoryStream(); var writer = XmlWriter.Create(stream); writer.WriteRaw(xml); stream.Position = 0; var fileStreamResult = File(stream, "application/octet-stream", "xml.xml"); return fileStreamResult; } 
0
source

You cannot pass an array of bytes, you need a Stream. Just pass the stream from your definition:

  public FileResult Download(string id) { string fid = Convert.ToString(id); var model = service.GetAllDefinitions().First(x => x.ID == id); var definitionDetails = new StatisticDefinitionModel(model); var definition = definitionDetails.ToXml; string fileName = definitionDetails.Name + ".xml"; string contentType = "text/xml"; return File(new MemoryStream(Encoding.Unicode.GetBytes(definition)), contentType, fileName); } 
0
source

All Articles