How to send by parameter of type string that contains special characters to the controller function?

2

In the view I have the following code:

 $("#btn_descargar").click(function (e) {
        e.preventDefault();
        debugger;
        var iBody = $("#iframeID").contents().find("body");
        var myContent = iBody.find("#myContent").text();      

        var url = '/Factura/DescargarXML?textoXML=' + myContent + '';
        window.location.href = url;
    });

In the controller:

 public ActionResult DescargarXML(String textoXML)
    {

        byte[] arr = System.Text.Encoding.ASCII.GetBytes(textoXML);
        var fileStreamResult = File(arr, "application/octet-stream", "DTE.xml");
        return fileStreamResult;
    }

But when the variable myContent (string that I sent as a parameter) contains characters like '<' or '>' (without quotes), it does not work for me. In this case I wanted to send a string that contained the following:

<?xml version="1.0" encoding="ISO-8859-1"?>
<EnvioDTE version="1.0" xmlns="http://www.sii.cl/SiiDte" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.sii.cl/SiiDte EnvioDTE_v10.xsd">
<SetDTE ID="ID78450470-0__1__33__137175">

I have tried to convert it to binary (byte []) in the view with javascript, but in doing so, it is separated by commas, which is impossible to receive parameters by commas in controller, since it says it can not find the resource.

The purpose of why I use window.location.href, is to download documents, in this case an xml.

If anyone has any idea how to send this data, I would really appreciate it

    
asked by Danilo 20.08.2018 в 18:16
source

1 answer

2

You must use an encoding that can pass special characters as part of the query in the url. Base64 is recommended for these cases. In javascript you can encode and decode Base64 with btoa () and atob () respectively. In C #:

Encode

public static string Base64Encode(string str) {
  var tb= System.Text.Encoding.UTF8.GetBytes(str);
  return System.Convert.ToBase64String(tb);
}

Decode

public static string Base64Decode(string base64Data) {
  var b= System.Convert.FromBase64String(base64Data);
  return System.Text.Encoding.UTF8.GetString(b);
}

Your functions

...
 var myContent = btoa(iBody.find("#myContent").text());      

public ActionResult DescargarXML(String str)
{

    byte[] arr = System.Text.Encoding.UTF8.GetBytes(Base64Decode(textoXML));
    var fileStreamResult = File(arr, "application/octet-stream", "DTE.xml");
    return fileStreamResult;
}
    
answered by 20.08.2018 / 18:31
source