Blue Theme Orange Theme Green Theme Red Theme
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Home | Forums | Videos | Photos | Blogs | Beginners | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Team Foundation Server Hosting
Search :       Advanced Search »
Home » VB.NET » Simple web File download in VB.NET

Simple web File download in VB.NET

This is a simple program that shows how to download files from the web and save them. The program uses the HttpWebRequest and HttpWebResponse classes to request and retrieve the requested file. The data are read into a buffer.

Page Views : 144683
Downloads : 0
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
Discover the top 5 tips for understanding .NET Interop
Become a Sponsor
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

Description:

This is a simple program that shows how to download files from the web and save them. The program uses the HttpWebRequest and HttpWebResponse classes to request and retrieve the requested file. The data are read into a buffer. A FileStream class is used to save the file to disk. In this example, a doppler radar map that covers the area I live is requested and saved to a file called "weather.jpg". Since the data are downloaded and read into the buffer asynchronously, a loop is required to read and keep track of how many bytes have been read, and the point in the stream where the next read should start. The loop will continue until the buffer is full or 0 bytes are read, indicating the end of the stream has been reached. The buffer must be sized large enough to hold the file. This is not a problem in this case as the doppler jpg's are a standard and known size.

Requirement:

Requires .NET SDK

How To Compile?

vbc /r:System.Net.dll /r:System.IO.dll webretrieve.vb.

Source Code

Imports System.IO
Imports System.Net
Imports System.Text
Class WebRetrieve
Public Shared Sub Main()
Dim wr As HttpWebRequest = CType(WebRequestFactory.Create("http://maps.weather.com/web/radar/us_orl_ultraradar_large_usen.jpg"), HttpWebRequest)
Dim ws As HttpWebResponse = CType(wr.GetResponse(), HttpWebResponse)
Dim str As Stream = ws.GetResponseStream()
Dim inBuf(100000) As Byte
Dim bytesToRead As Integer = CInt(inBuf.Length)
Dim bytesRead As Integer = 0
While bytesToRead > 0
Dim n As Integer = str.Read(inBuf, bytesRead, bytesToRead)
If n = 0 Then
Exit While
End If
bytesRead += n
bytesToRead -= n
End While
Dim fstr As New FileStream("weather.jpg", FileMode.OpenOrCreate, FileAccess.Write)
fstr.Write(inBuf, 0, bytesRead)
str.Close()
fstr.Close()
End Sub 'Main
End Class 'WebRetrieve

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Bill Farley
Bill Farley is an engineer. He enjoys programming as a hobby.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the top 5 tips for understanding .NET
Ricky Leeks presents the top 5 tips for understanding .NET Interoperability. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
 Comments
Code doesnt work by Abdul On April 25, 2009
Code doesnt work
Reply | Email | Modify 
Re: Code doesnt work by Paul On July 31, 2009
It works great, just replace WebRequestFactory with WebRequest...
Reply | Email | Modify 
Removes lines from text file by Philip On May 9, 2010
Great code but when I download a text file it removes the lines and places all the text on the first line of the text file. Any ideas?
Reply | Email | Modify 
DENIED by Atsuko On December 21, 2010
It downloads the file, but I can't get it to write to the client machine! Also, changed HttpWebRequest to FileWebRequest/response, etc, since I'm downloading from a database server. I have the filepath where I want it to write hard coded, but: A: I want the thing to open a file save dialog B: That's about all.
Reply | Email | Modify 
Need to open Save dialog by DotnetProg On February 10, 2011
hi, can i open save dialog??
Reply | Email | Modify 
Dowload multiple unknown filenames from HTTP site by Simone On April 13, 2011
Is it possible to download multiple csv files from an HTTP site without knowing what the filenames are? I need to get a list of the variable filenames and loop through them and download them one at a time. Or is there a way that I can download all of them to a directory keeping all their filenames the same somehow?
Reply | Email | Modify 
Discover the top 5 tips for understanding .NET Interop
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.