简体   繁体   English

使用api下载.xlsx文件会返回XML格式的文件? VB.NET

[英]Download .xlsx file using an api returns XML formatted file? VB.NET

I am attempting to download a .xlsx file from a web application using an API. 我正在尝试使用API​​从Web应用程序下载.xlsx文件。 However, the string content of the file is always scrambled and seems to have XML content in it. 但是,文件的字符串内容始终是混乱的,并且似乎包含XML内容。 I'm assuming its XML since its got '[Content_Types].xml' mentioned in the first line. 我假设它是XML,因为在第一行中提到了“ [Content_Types] .xml”。

The response headers mention that the content-type being returned is 'application/octet-stream'. 响应头提到返回的内容类型是“ application / octet-stream”。 I tried adding content-type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" in my request headers, but the operation was 'not recognized' by the web application. 我尝试在请求标头中添加content-type =“ application / vnd.openxmlformats-officedocument.spreadsheetml.sheet”,但该Web应用程序“无法识别”该操作。 So I cant return .xlsx type files... 所以我不能返回.xlsx类型的文件...

When I try downloading this file and saving it as .xlsx, I cant open it since it always says that the file is corrupt. 当我尝试下载此文件并将其另存为.xlsx时,无法打开它,因为它始终表示文件已损坏。 However, using Postman to download the file works with no corruption. 但是,使用Postman下载文件不会损坏。 I'm not sure where I am going wrong while downloading and saving the binary 64 base encoded data. 我不确定在下载和保存二进制64基编码的数据时哪里出了问题。 Please help! 请帮忙! Here is the code I'm using do download and save file. 这是我正在使用的代码,用于下载和保存文件。

If My.Computer.FileSystem.DirectoryExists(DownloadLocation) = False Then
        MsgBox("Folder path '" & DownloadLocation & "' does not exist.", MsgBoxStyle.Information)
        Return
    End If

    Dim url As String = 'I am setting the URL here, tested on postman and no issues here 

Getting filename from the response header using a function, No issues with the filepath and name. 使用函数从响应头获取文件名,文件路径和名称没有问题。

 Dim Filepath As String = DownloadLocation & "\" & filename.Split(".").First & "_" & Format(Now, "yyyymmdd hhmmss") & "." & filename.Split(".").Last

    Dim credentials As String = ""
    credentials = "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(LoginName + ":" + PW))
    Dim Request As HttpWebRequest = DirectCast(HttpWebRequest.Create(url), HttpWebRequest)
    With Request
        .Headers.Set(HttpRequestHeader.Authorization, credentials)
        .Headers.Set("X-Application-Key", My.Settings.APIKey)
        .Method = "GET"
        .AutomaticDecompression = DecompressionMethods.GZip
    End With

    Try
        Dim response As HttpWebResponse
        response = Request.GetResponse
        Dim stream As Stream = response.GetResponseStream
        Dim reader As New StreamReader(stream)
        Dim Ofile As FileStream = New FileStream(Filepath, FileMode.Create)
        Dim Owrite As StreamWriter = New StreamWriter(Ofile)
        Owrite.Write(reader.ReadToEnd)
        reader.Close()
        Owrite.Close()
        Ofile.Close()
    Catch ex As Exception
        MsgBox("Download failed..." & vbNewLine & vbNewLine & ex.ToString, MsgBoxStyle.Information)
        Return
    End Try

The file is saved as a .xlsx file, but when I try to open the file, Excel says that the file is corrupted. 该文件另存为.xlsx文件,但是当我尝试打开该文件时,Excel表示该文件已损坏。 Anyone know whats happening here? 有人知道这里发生了什么吗?

Remove the GZip decompression. 删除GZip减压。 First, you don't need it. 首先,您不需要它。 Second, it's causing you this trouble. 其次,这给您带来了麻烦。

XLSX file is set of XML files zipped into one file. XLSX文件是一组压缩成一个文件的XML文件。 And renamed to .xlsx extension. 并重命名为.xlsx扩展名。 Take any of your xlsx files, rename it to zip and unzip the content. 提取任何xlsx文件,将其重命名以压缩并解压缩内容。 You'll see the XML structure and it's directories. 您将看到XML结构及其目录。

AutomaticDecompression propably detects the *ZIP in the response stream bytes and unzip its for you on the client side. AutomaticDecompression可以在响应流字节中正确检测* ZIP,并在客户端为您解压缩。

GZipping Zipped content is not needed, because compressing compressed file makes it lager in most of cases. GZipping不需要压缩的内容,因为在大多数情况下,压缩压缩文件会使文件更大。 Since the entropy of the message is already maximalized. 由于消息的熵已经最大化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM