4

So I am using a third-party service that lets me edit my XML file that is stored on the server path. Now once I am done editing my XML, I save the file to local memory storage that generates a BLOB appended to a URL.

Example:

blob:http://localhost/0e06af7a-a1a9-4bf1-a79a-78a1e107648f

Where the 0e06af7a-a1a9-4bf1-a79a-78a1e107648f is the token created for the current edit. Now when I run the above URL in a browser, I can see:

enter image description here

My question is: How can I read the above URL using C# and then save the content to an object that I can later use to upload to a file or a cloud. I have tried using the WebClient:

WebClient client = new WebClient();
Stream stream = client.OpenRead("blob:http://localhost/0e06af7a-a1a9-4bf1-a79a-78a1e107648f");
StreamReader reader = new StreamReader(stream);
string str= reader.ReadToEnd();

But it gives me an error saying that the URL is not correct and should start with HTTP or HTTPS.

Edit: I am able to save the blob to a file using JQuery:

var download = $('<a>Download ' + "file.xml" + '</a>').attr("href", "blob:http://localhost/0e06af7a-a1a9-4bf1-a79a-78a1e107648f");
download.attr("download", "file.xml");

This successfully creates a file called file.xml and downloads the file. I am looking to save this blob content on the server side so I can send it to a Amazon S3 bucket.

Again Editing:

So, currently I have saved the XML as a string and I am trying to send it to the C# Controller via AJAX but running into the 500 internal server error.

        var xmlString = self.xml2Str(self.xmlState.xml);
        //console.log(xmlString);
        var blob = new Blob([xmlString], { type: "text/xml" }); 
        console.log(blob);
        var url = URL.createObjectURL(blob);
        console.log(url);
        var json = {
            xmlString: xmlString
        };

        var test = JSON.stringify(json);
        console.log(test);

        try {
            $.ajax({
                url: BaseURL + "Home/SendXMLToS3",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                data: { "json": test},
                type: "POST",
                success: function (data) {
                    //TODO: Add whatever if you want to pass a notification back
                    alert("Done");
                },
                error: function (error) {
                    //TODO: Add some code here for error handling or notifications
                    alert("Not Done");
                }
            });
        }
        catch (err) {
            console.log(err);
        }

The contents of test variable are as follows (from the console):

{"xmlString":"<nitf>\n  <head>\n    <docdata>\n      <identified-content>\n        <classifier id=\"box-geometry\" value=\"147,623,250,790\" />\n        <classifier id=\"uuid\" value=\"Mv8XVAViEeqyc3SUunSxMg\" />\n      </identified-content>\n    </docdata>\n  </head>\n  <body>\n    <body.head />\n    <body.content>\n      <p>\n        <lang fontStyle=\"regular\" style=\".Bodylaser\">How is this different from Pegasus?</lang>\n      </p>\n      <p>\n        <lang fontStyle=\"regular\" style=\".Bodylaser\">Pegasus could be installed on your phone without your knowledge through just a missed WhatsApp video call. In the case of the new breach, a user has to manually download the MP4 file sent to them before any malicious code can be run. The problem is not with WhatsApp’s end-to-end encryption feature here. Getting remote access to your phone is the equivalent of actually holding it in one’s hand. End-to-end encryption is meant to stop attackers from stealing or snooping on chats in between. So, unless someone has access to your device, they can’t actually read your chats, even if they intercept them.</lang>\n      </p>\n    </body.content>\n  </body>\n</nitf>"}

Also tried setting the default ASP.NET has request validation enabled by default to help protect against XSS to false.

    [HttpPost]
    [ValidateInput(false)]
    public ActionResult SendXMLToS3(string json)

Still the same 500 error persists: jquery.min.js:4 POST http://localhost/DEGit/Home/SendXMLToS3 500 (Internal Server Error) in the h.send(c.hasContent&&c.data||null) method.

How can I:

  1. Read the generated blob URL content in C#?
  2. Send the test string via AJAX to C#?
  3. Anything else that you guys can suggest.

Thanks

15
  • What happens when you remove that "blob:", leaving just "http://..."? Jan 30, 2020 at 12:09
  • @HansKesting The URL is not found. It is basically a in-memory blob that is created during run time and destroyed as soon as the process is complete. I managed to send this URL to my Controller to be processed but I am not sure how to read the contents from this Jan 30, 2020 at 12:11
  • You do not have XML, it is HTML. You cannot read HTML with XML methods. HTML and XML are similar and only some HTML will read with XML tools.
    – jdweng
    Jan 30, 2020 at 12:15
  • 1
    @jdweng Not true, the XML is serialized and appended to this URL. It is something like serializing a XML and saving it to a file. In this case, the method appends the blob to a in-memory URL Jan 30, 2020 at 12:16
  • 1
    @jawad Ouch. That would take a while since it is a long intergrated process. If you could try sending the test variable to a Controller, that would be great. Jan 30, 2020 at 14:53

1 Answer 1

1

AS you requested in the comments: I made simple controller and JS code to hit an httppost method within the controller using ajax:

  1. The Controller Method:
[HttpPost]
public JsonResult SendXMLToS3(string json)
{
    return Json(new { Result = "PASS", Message = "testing SendXMLToS3" });
}
  1. JS code:
<script type="text/javascript">
    function myfunction() {

        var test = { "xmlString": "<nitf>...</nitf>" };
        $.ajax({
            type: "post",
            url: "/Home/SendXMLToS3",
            dataType: "json",
            data: { "json": test },
            success: function (res) {
                alert(res.Message);
            },
            error: function (res) {
                //TODO: Add some code here for error handling or notifications
                alert("Not Done");
            }
        });
    }
</script>

Findings:
The line contentType: "application/json; charset=utf-8", was causing the Internal 500 error, if you remove it, you should be fine.

2
  • Yes, that worked. Thanks a lot. Now I am able to send the string to the Controller and perform the required actions. Any idea on how to read the blob url string into the Controller method of c# ? Jan 31, 2020 at 7:22
  • 1
    Regarding the blob url, please read the following explanation of blob urls: superuser.com/a/1109873/102549 in summary blob data can't be accessed at controller level directly "server side" it need to be rendered and exec first by JS engine. Feb 2, 2020 at 12:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.