Get file size before download it in vb - vb.net

I have been working on a web browser in visual basic..Now,what I want to do is to the get file size before download it and when I click download I want to to get the number of the alrady downloaded Mbs(watch the picture)
Thank's for help!

I've done some research, and this would probably be the most simple and "cleanest" way of getting a download's size (in bytes):
Public Function GetDownloadSize(ByVal URL As String) As Long
Dim r As Net.WebRequest = Net.WebRequest.Create(URL)
r.Method = Net.WebRequestMethods.Http.Head
Using rsp = r.GetResponse()
Return rsp.ContentLength
End Using
End Function
Credit to Reed Kimble, who told me to dispose the WebResponse in my initial MSDN question.
The above code will read the response headers of the file, rather than reading the body of it. This means that the file does not require to get downloaded, just to check it's size.
This is the reason to why some codes requires the file to actually get downloaded at first; they're reading the file's body rather than it's headers.
Hope this helps!

Use the WebClient ResponseHeaders:
Public Shared Function GetFileSize(url As String) As Long
Using obj As New WebClient()
Using s As Stream = obj.OpenRead(url)
Return Long.Parse(obj.ResponseHeaders("Content-Length").ToString())
End Using
End Using
End Function
Request file size before download it

The WebClient's DownloadProgressChanged event's args contains the property TotalBytesToRecieve. That tells you how many bytes the file you're downloading is.
Not the prettiest way, but if you want to get the size of the file before downloading you can start downloading the file then immediately cancel it:
Dim DownloadSize As Long
Private Sub CheckDownloadSize(ByVal URL As String)
WebClient.DownloadFile(URL, IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Temp, "tempdownload.tmp"))
End Sub
Private WithEvents WebClient As New WebClient
Private Sub WebClient_DownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles WebClient.DownloadProgressChanged
DownloadSize = e.TotalBytesToReceive
WebClient.CancelAsync()
End Sub
Otherwise, just remove the .CancelAsync() line.

Related

VB.net POST method using HTTPclient

Hello I am trying to access a webform with data. However my code does not have error but its not working. It is supposed to send me a text message using the Web API provided by the telco carrier. Any assistance and advise will be greatly appreciated.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
Dim client As New HttpClient
Dim url = $"some url"
Dim data2 = "{""username"": ""someuname"", ""password"":""somepass"", ""msisdn:""some number"", ""content:""Hello, this is a sample broadcast"", ""shortcode_mask:""somemask""}"
Dim payload = Newtonsoft.Json.JsonConvert.SerializeObject(data2)
Dim buffer = Encoding.UTF8.GetBytes(payload)
Dim bytes = New Net.Http.ByteArrayContent(buffer)
bytes.Headers.ContentType = New Net.Http.Headers.MediaTypeHeaderValue("application/json")
Dim request = client.PostAsync(url, bytes)
Catch Ex As Exception
Console.WriteLine()
End Try
End Sub
End Class
There are a few issues in your code. Let me start with the most important one first:
Catch Ex As Exception
Console.WriteLine()
End Try
This code means: If an error occurs, throw away all useful information about the error that could help me find the cause, then write an empty line to the console and pretend nothing bad happened.
Don't do that. Remove the whole try-catch block, then re-run your code again. If you get an exception: That's great, because .NET is now telling you what is wrong with your code! If you understand the error message, use it to fix your code. If you don't understand it, feel free to ask the community on StackOverflow.
Second issue:
This line
Dim request = client.PostAsync(url, bytes)
starts an asynchronous web request. You don't wait for the result, so you don't know whether it succeeded or not.
You're supposed to Await async methods, but if you aren't familiar with the async/await pattern yet, I won't be able to give you all the necessary background in the single StackOverflow answer. Until you have familiarized youself with async/await, you can synchronously wait for the result by accessing the Result property of the Task that has been returned:
Dim response = client.PostAsync(url, bytes).Result
response.EnsureSuccessStatusCode()
Third issue: You JSON-serialize something which is already JSON. If your data is already JSON, just use it as the payload without calling SerializeObject.
Your data2 is a string. When you use the JSON library to serialize it, it probably isn't in the JSON structure that your URL endpoint expects. Create a Type, fill it with data, and pass that to your JsonCoverter
Thank you Heinzi, here is the final working code based on your concepts:
Public Class Form1
Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim client As New HttpClient
Dim url = $"some url"
Dim data2 = "
{""username"": ""some uname"", ""password"": ""somepass"", ""msisdn"": ""somenumber"", ""content"": ""Hello, this is a sample broadcast"", ""shortcode_mask"" :""somemask""}"
Dim buffer = Encoding.UTF8.GetBytes(data2)
Dim bytes = New ByteArrayContent(buffer)
bytes.Headers.ContentType = New Headers.MediaTypeHeaderValue("application/json")
Dim request1 = Await client.PostAsync(url, bytes)
Dim response = client.PostAsync(url, bytes).Result
response.EnsureSuccessStatusCode()
End Sub
End Class
I can now receive text messages :)

ActiveReports not allowing overwrite of jpg after report generation. Windows. VB.NET

Currently, I am utilizing ActiveReports to implement a dynamic image via pathname into a report that is generating.
The Images are being automatically generated as .jpg to a server folder. The Active Reports module imports the files using this code.
Sub ActiveReport_ReportStart
Picture1.Image = System.Drawing.Image.FromFile("path\filename.jpg")
End Sub
The problem I am running into is that this report locks out the jpgs from being overwritten.
I am unsure what could be going wrong, but it seems that the report is still using the image files after the report has been generated.
Am I missing a "disconnect" code piece to ensure that an import doesn't allow for continued contact to the file?
I apologize if this is simple, but I can't find anything for this specific instance.
Thank you.
EDIT:
I attempted to get around the lockout by copying them into their own variable. But this didn't work either.
Sub ActiveReport_ReportStart
dim TempImage as Image = Image.FromFile("path\filename")
Picture1.Image = TempImage
End Sub
You can use "Using" block to make sure that the object of the image is disposed as soon as its usage is completed.
Using statement basically marks a boundary for the objects specified in the statement. So when code block using Using – End Using is exited either after normal execution or some exception cause, the framework invokes the Dispose method of these objects automatically.
Here is the suggested code which can be helpful for you in resolving the issue:
Private Sub SectionReport1_ReportStart(sender As Object, e As EventArgs) Handles MyBase.ReportStart
Dim img As Image
Using bmpTemp = New Bitmap("path\filename.jpg")
img = New Bitmap(bmpTemp)
End Using
Picture1.Image = img
End Sub
I was able to get this to work by creating a function that used the Graphics.FromImage method and disposed the original file.
Public Function GetImageFile(ByVal pathfn As String) As Image
Dim tempImg As Image = Image.FromFile(pathfn)
Dim tempBtm As New Bitmap(Width:=img.Width*CorrectFactor, Height:=img.Height*CorrectFactor, Format:=tempImg.PixelFormat)
Using g As Graphics = Graphics.FromImage(bm)
g.DrawImage(tempImg, Point.Empty)
End Using
tempImg.Dispose()
Return tempBtm
End Function
The item that would be placed in the report would be as follows.
Sub ActiveReport_ReportStart
Picture1.Image = GetImageFile("Path\Filename")
End Sub

Check if downloaded file exists by comparing it's file size

Is there a way to check a downloaded file is already exists by comparing it's file size?
Below is my download code.
Private Sub bgw_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs)
Dim TestString As String = "http://123/abc.zip," & _
"http://abc/134.zip,"
address = TestString.Split(CChar(",")) 'Split up the file names into an array
'loop through each file to download and create/start a new BackgroundWorker for each one
For Each add As String In address
'get the path and name of the file that you save the downloaded file to
Dim fname As String = IO.Path.Combine("C:\Temp", IO.Path.GetFileName(add))
My.Computer.Network.DownloadFile(add, fname, "", "", False, 60000, True) 'You can change the (False) to True if you want to see the UI
'End If
Next
End Sub
The size of a local file can be determined using the File or FileInfo class from the System.IO namespace. To determine the size of a file to be downloaded by HTTP, you can use an HttpWebRequest. If you set it up as though you were going to download the file but then set the Method to Head instead of Get you will get just the response headers, from which you can read the file size.
I've never done it myself, or even used an HttpWebRequest to download a file, so I'm not going to post an example. I'd have to research it and you can do that just as easily as I can.
Here's an existing thread that shows how it's done in C#:
How to get the file size from http headers
Here's a VB translation of the code from the top answer:
Dim req As System.Net.WebRequest = System.Net.HttpWebRequest.Create("https://stackoverflow.com/robots.txt")
req.Method = "HEAD"
Using resp As System.Net.WebResponse = req.GetResponse()
Dim ContentLength As Integer
If Integer.TryParse(resp.Headers.Get("Content-Length"), ContentLength)
'Do something useful with ContentLength here
End If
End Using
A better practice would be to write this line:
req.Method = "HEAD"
like this:
req.Method = System.Net.WebRequestMethods.Http.Head

Zipfile in vb.net 4.5 "The directory name is invalid." error - can't identify the cause?

I've got a small utility that can encrypt/decrypt using .net 4.5 TripleDES routines. I added compression and found the simpler compression stuff doesn't work for me.
The files referenced in these fields definitely exist, aren't huge at all and are even named in the same case.
Yet I keep getting "The directory name is invalid.". If I take out the test for existence of the archive, I get a different error - as it's made the zip and it's 0 bytes. I've searched for far longer on this than it took to work out how to use the encryption part of my utility yesterday - and that's with learning how streams work!
Can anyone shed light on this issue at all please?
Private Encoded As String = "C:\TestFile\encoded.txt"
Private Decoded As String = "C:\TestFile\decoded.txt"
Private CompressEncoded As String = "C:\TestFile\encoded.zip"
Private Sub Main()
Compress(Encoded, CompressEncoded)
End sub
Sub Compress(filename As String, zippedFile As String)
'Added to handle file already exists error
If IO.File.Exists(zippedFile) Then IO.File.Delete(zippedFile)
If IO.File.Exists(filename) Then IO.Compression.ZipFile.CreateFromDirectory(filename, zippedFile, CompressionLevel.Fastest, True)
End Sub
In addition to this code I also tried adding a quick streamreader test to prove to myself that filename does exist.
' Dim sr As New StreamReader(filename)
MessageBox.Show(sr.ReadToEnd)
It does and displays the line of text within it.
I'd be grateful for any help - this rather silly bug has chewed up lots of time this afternoon that I was hoping to use on something more useful :).
Thanks folks!
Thanks for the answer!
Sub Compress(filename As String, zippedFile As String)
If IO.File.Exists(zippedFile) Then IO.File.Delete(zippedFile)
If IO.File.Exists(filename) Then
Using archive As ZipArchive = Open(zippedFile, ZipArchiveMode.Create)
archive.CreateEntryFromFile(filename, Path.GetFileName(filename), CompressionLevel.Fastest)
End Using
End If
End Sub
Sub Decompress(ZippedFile As String, UnzippedFile As String)
If IO.File.Exists(UnzippedFile) Then IO.File.Delete(UnzippedFile)
If IO.File.Exists(ZippedFile) Then
Using archive As ZipArchive = Open(ZippedFile, ZipArchiveMode.Read)
archive.ExtractToDirectory(Path.GetDirectoryName(UnzippedFile))
End Using
End If
End Sub
Probably it is a simple error.
The first parameter to pass to CreateFromDirectory should be a directory name, but you pass a file name.
Try with
If IO.File.Exists(filename) Then
IO.Compression.ZipFile.CreateFromDirectory(Path.GetDirectoryName(filename), _
zippedFile, CompressionLevel.Fastest, True)
End If
If you want to compress a single file, use ZipArchive class like below:
Sub Compress(filename As String, zippedFile As String)
If IO.File.Exists(zippedFile) Then IO.File.Delete(zippedFile)
If IO.File.Exists(filename) Then
Using archive As ZipArchive = ZipFile.Open(zippedFile, ZipArchiveMode.Create)
archive.CreateEntryFromFile(filename, Path.GetFileName(filename), CompressionLevel.Fastest)
End Using
End If
End Sub
ADDITIONAL INFO
Need to add reference to System.IO.Compression.dll for the ZipArchive and System.IO.Compression.FileSystem.dll for the ZipFile class. These DLLs are not referenced by default when a project is created in VS.

Download URL Contents Directly into String (VB6) WITHOUT Saving to Disk

Basically, I want to download the contents of a particular URL (basically, just HTML codes in the form of a String) into my VB6 String variable. However, there are some conditions.
I know about the URLDownloadToFile Function - however, this requires that you save the downloaded file/HTML onto a file location on disk before you can read it into a String variable, this is not an option for me and I do not want to do this.
The other thing is, if I need to use an external library, it must already come with all versions of Windows from XP and onwards, I cannot use a control or library that I am required to ship, package and distribute even if it is free, this is not an option and I do not want to do this. So, I cannot use the MSINET.OCX (Internet Transfer) Control's .OpenURL() function (which simply returns contents into a String), as it does not come with Windows.
Is there a way to be able to do this with the Windows API, URLMON or something else that is pre-loaded into or comes with Windows, or a way to do it in VB6 (SP6) entirely?
If so, I would appreciate direction, because even after one hour of googling, the only examples I've found are references to URLDownloadToFile (which requires saving on disk before being ale to place into a String) and MsInet.OpenURL (which requires that I ship and distribute MSINET.OCX, which I cannot and don't want to do).
Surely there has got to be an elegant way to be able to do this? I can do it in VB.NET without an issue, but obviously don't have the luxury of the .NET framework in VB6 - any ideas?
Update:
I have found this: http://www.freevbcode.com/ShowCode.asp?ID=1252
however it says that the displayed function may not return the entire
page and links to a Microsoft bug report or kb article explaining
this. Also, I understand this is based off wininet.dll - and I'm
wondering which versions of Windows does WinInet.dll come packaged
with? Windows XP & beyond? Does it come with Windows 7 and/or Windows
8?
This is how I did it with VB6 a few years ago:
Private Function GetHTMLSource(ByVal sURL As String) As String
Dim xmlHttp As Object
Set xmlHttp = CreateObject("MSXML2.XmlHttp")
xmlHttp.Open "GET", sURL, False
xmlHttp.send
GetHTMLSource = xmlHttp.responseText
Set xmlHttp = Nothing
End Function
If you want to do this with pure VB, and no IE, then you can take advantage of a little-used features of the VB UserControl - async properties.
Create a new UserControl, and call it something like UrlDownloader. Set the InvisibleAtRuntime property to True. Add the following code to it:
Option Explicit
Private Const m_ksProp_Data As String = "Data"
Private m_bAsync As Boolean
Private m_sURL As String
Public Event AsyncReadProgress(ByRef the_abytData() As Byte)
Public Event AsyncReadComplete(ByRef the_abytData() As Byte)
Public Property Let Async(ByVal the_bValue As Boolean)
m_bAsync = the_bValue
End Property
Public Property Get Async() As Boolean
Async = m_bAsync
End Property
Public Property Let URL(ByVal the_sValue As String)
m_sURL = the_sValue
End Property
Public Property Get URL() As String
URL = m_sURL
End Property
Public Sub Download()
UserControl.AsyncRead m_sURL, vbAsyncTypeByteArray, m_ksProp_Data, IIf(m_bAsync, 0&, vbAsyncReadSynchronousDownload)
End Sub
Private Sub UserControl_AsyncReadComplete(AsyncProp As AsyncProperty)
If AsyncProp.PropertyName = m_ksProp_Data Then
RaiseEvent AsyncReadComplete(AsyncProp.Value)
End If
End Sub
Private Sub UserControl_AsyncReadProgress(AsyncProp As AsyncProperty)
If AsyncProp.PropertyName = m_ksProp_Data Then
Select Case AsyncProp.StatusCode
Case vbAsyncStatusCodeBeginDownloadData, vbAsyncStatusCodeDownloadingData, vbAsyncStatusCodeEndDownloadData
RaiseEvent AsyncReadProgress(AsyncProp.Value)
End Select
End If
End Sub
To use this control, stick it on a form and use the following code:
Option Explicit
Private Sub Command1_Click()
XDownload1.Async = False
XDownload1.URL = "http://www.google.co.uk"
XDownload1.Download
End Sub
Private Sub XDownload1_AsyncReadProgress(the_abytData() As Byte)
Debug.Print StrConv(the_abytData(), vbUnicode)
End Sub
Suffice to say, you can customise this to your hearts content. It can tell (using the AyncProp object) whether the file is cached, and other useful information. It even has a special mode in which you can download GIF, JPG and BMP files and return them as a StdPicture object!
One alternative is using Internet Explorer.
Dim ex As InternetExplorer
Dim hd As HTMLDocument
Dim s As String
Set ex = New InternetExplorer
With ex
.Navigate "http://donttrack.us/"
.Visible = 1
Set hd = .Document
s = hd.body.innerText ' assuming you just want the text
's = hd.body.innerHTML ' if you want the HTML
End With
EDIT: For the above early binding to work you need to set references to "Microsoft Internet Controls" and "Microsoft HTML Object Library" (Tools > References). You could also use late binding, but to be honest, I forget what the proper class names are; maybe someone smart will edit this answer :-)