【发布时间】:2020-05-16 01:10:42
【问题描述】:
所以我正在使用第三方服务,它可以让我编辑存储在服务器路径上的 XML 文件。现在,一旦我完成了对 XML 的编辑,我会将文件保存到本地内存存储中,该存储会生成一个附加到 URL 的 BLOB。
例子:
blob:http://localhost/0e06af7a-a1a9-4bf1-a79a-78a1e107648f
0e06af7a-a1a9-4bf1-a79a-78a1e107648f 是为当前编辑创建的令牌。现在,当我在浏览器中运行上述 URL 时,我可以看到:
我的问题是:如何使用 C# 读取上述 URL,然后将内容保存到一个对象中,以便以后用于上传到文件或云中。我试过使用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();
但它给了我一个错误,说URL 不正确,应该以HTTP 或HTTPS 开头。
编辑:我可以使用 JQuery 将 blob 保存到文件中:
var download = $('<a>Download ' + "file.xml" + '</a>').attr("href", "blob:http://localhost/0e06af7a-a1a9-4bf1-a79a-78a1e107648f");
download.attr("download", "file.xml");
这会成功创建一个名为file.xml 的文件并下载该文件。我希望将此 blob 内容保存在服务器端,以便将其发送到 Amazon S3 存储桶。
再次编辑:
所以,目前我已将XML 保存为字符串,并尝试通过 AJAX 将其发送到 C# 控制器,但遇到 500 内部服务器错误。
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);
}
test 变量的内容如下(来自控制台):
{"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>"}
还尝试将默认 ASP.NET 设置为默认启用请求验证,以帮助防止 XSS 为 false。
[HttpPost]
[ValidateInput(false)]
public ActionResult SendXMLToS3(string json)
同样的 500 错误仍然存在:jquery.min.js:4 POST http://localhost/DEGit/Home/SendXMLToS3 500 (Internal Server Error) 在h.send(c.hasContent&&c.data||null) 方法中。
我该怎么做:
- 用C#读取生成的
blobURL内容? - 通过 AJAX 将
test字符串发送到 C#? - 你们还有什么可以建议的。
谢谢
【问题讨论】:
-
当您删除“blob:”,只留下“http://...”时会发生什么?
-
@HansKesting 找不到 URL。它基本上是一个内存中的 blob,在运行时创建并在进程完成后立即销毁。我设法将此 URL 发送到我的控制器以进行处理,但我不确定如何从中读取内容
-
你没有 XML,它是 HTML。您无法使用 XML 方法读取 HTML。 HTML 和 XML 相似,只有部分 HTML 可以用 XML 工具读取。
-
@jdweng 不正确,XML 被序列化并附加到此 URL。这类似于序列化 XML 并将其保存到文件中。在这种情况下,该方法将 blob 附加到内存中的 URL
-
@jawad 哎哟。这需要一段时间,因为这是一个长期的综合过程。如果您可以尝试将
test变量发送到控制器,那就太好了。
标签: c# ajax http blob asp.net-ajax