i have this C# code for download xlsx document from url
var request = (HttpWebRequest)WebRequest.Create("url to xlsx file");
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
request.Headers[HttpRequestHeader.AcceptEncoding] = "gzip,deflate";
using (var response = request.GetResponse())
using (var stream = response.GetResponseStream())
using (var output = File.Create("H:\\doc.xlsx"))
{
stream.CopyTo(output);
}
my target programming language is VB.net so i changed C# to vb like below
Dim request As HttpWebRequest = WebRequest.Create("url to xlsx file")
request.AutomaticDecompression = DecompressionMethods.GZip Or DecompressionMethods.Deflate
request.Headers(HttpRequestHeader.AcceptEncoding) = "gzip,deflate"
Dim response As WebResponse = request.GetResponse()
Dim stream As Stream = response.GetResponseStream()
Dim output As FileStream = File.Create("H:\doc.xlsx")
stream.CopyTo(output)
xlsx file that is downloaded by VB is corrupted and unreadable but C# works well what is the mistake?
Try including the using
statements: It is possible the whole content has not been flushed to the output so you get the corrupt message.
Using response = request.GetResponse()
Using stream = response.GetResponseStream()
Using output = File.Create("H:\doc.xlsx")
stream.CopyTo(output)
End Using
End Using
End Using
The using
block will make sure to dispose of the objects, which you should be doing regardless of the issue of ending with a corrupted file, and it will also call Close()
method on the underlying to stream and any data previously written to the buffer will be copied to the file before the file stream is closed,