尝试下载XML文件时出错

时间:2023-02-06 07:57:00

I'm trying to download an XML file, the file already exists in the specified path, I am not familiar with VB and probably this code is not right, I need help just in it to be able to download an existing xml file, here's the code:

我正在尝试下载一个XML文件,该文件已经存在于指定的路径中,我不熟悉VB,可能这个代码不对,我需要帮助才能下载现有的xml文件,这里是代码:

Protected Sub DownloadFile(ByVal sPath As String)
        Dim TargetFile As New System.IO.FileInfo(sPath)
        Response.Clear()
        Response.AddHeader("Content-Disposition", "attachment; filename=" +
            TargetFile.Name)
        Response.AddHeader("Content-Length", TargetFile.Length.ToString())
        Response.ContentType = "text/xml"
        Response.WriteFile(TargetFile.FullName)
        Response.End()
End Sub

The error returned in the console:

控制台中返回的错误:

Uncaught Error: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. Common causes for this error are when the response is modified by calls to Response.Write(), response filters, HttpModules, or server trace is enabled.

未捕获错误:Sys.WebForms.PageRequestManagerParserErrorException:无法解析从服务器收到的消息。此错误的常见原因是通过调用Response.Write(),响应过滤器,HttpModules或服务器跟踪来修改响应。

Contextualizing the problem:

将问题情境化:

I have serialized an object and created an XML file, then I would simply like to download this file, my difficulty is to download the file.

我已经序列化了一个对象并创建了一个XML文件,然后我只想下载这个文件,我的困难是下载文件。

Dim oObj1 As New System.Xml.Serialization.XmlSerializer(GetType(eSocial.Eventos.evtTabHorTur.eSocial))
Dim sFileName = Date.Now.ToString("yyyyMMddHHmmss") & ".xml"
Dim sPath = Constantes.Ambiente.CaminhoSite & "temp\" & sFileName
Dim oFile As New System.IO.StreamWriter(sPath)
oObj1.Serialize(oFile, eSocialCamposXml)
oFile.Close()

1 个解决方案

#1


0  

You are saying you are having difficulty downloading but there is nothing in the code except showing writing a file and then serializing a file. You would be using a 'StreamReader' or similar manner to READ a file. Here is a simple example. Say I have an xml structure on a file location with the schema like:

你说你下载有困难,但除了显示文件然后序列化文件外,代码中没有任何内容。您将使用“StreamReader”或类似方式来读取文件。这是一个简单的例子。假设我在文件位置上有一个xml结构,其架构如下:

<root>
    <test>Data</test>
</root>

I could write this in VB.NET to get it:

我可以在VB.NET中编写它来获取它:

Sub Main()
  Dim xmlFile As XDocument
  Dim fileLocation = "D:\\Test Code\\Test.xml"
  Using sr = New StreamReader(fileLocation)
    xmlFile = XDocument.Parse(sr.ReadToEnd())
  End Using

  Console.WriteLine(xmlFile.Root.Element("test").Value.ToString)

  Console.ReadLine()
End Sub

#1


0  

You are saying you are having difficulty downloading but there is nothing in the code except showing writing a file and then serializing a file. You would be using a 'StreamReader' or similar manner to READ a file. Here is a simple example. Say I have an xml structure on a file location with the schema like:

你说你下载有困难,但除了显示文件然后序列化文件外,代码中没有任何内容。您将使用“StreamReader”或类似方式来读取文件。这是一个简单的例子。假设我在文件位置上有一个xml结构,其架构如下:

<root>
    <test>Data</test>
</root>

I could write this in VB.NET to get it:

我可以在VB.NET中编写它来获取它:

Sub Main()
  Dim xmlFile As XDocument
  Dim fileLocation = "D:\\Test Code\\Test.xml"
  Using sr = New StreamReader(fileLocation)
    xmlFile = XDocument.Parse(sr.ReadToEnd())
  End Using

  Console.WriteLine(xmlFile.Root.Element("test").Value.ToString)

  Console.ReadLine()
End Sub