Rahul Sharma
Rahul Sharma

Reputation: 8311

Reading a BLOB that is appended to HTTP extension using C#

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

Upvotes: 4

Views: 2455

Answers (1)

Jawad Al Shaikh
Jawad Al Shaikh

Reputation: 2735

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.

Upvotes: 1

Related Questions