Solved

Download/Upload a file - HTTP, FTP or something else?

Posted on 2004-08-16
7
153 Views
Last Modified: 2010-04-23
Problems:

1. I need to download a text file from a server (not sure if it will be a web or file server) and extract the first line of the text file (the text file will actually only have one line). I know the directory and filename of the text file. The server is a Windows 2000 Server.

2. I need to upload a bitmap image to a server (again, not sure if it will be a web or file server). I know where the bitmap image is supposed to be saved. I may have to upload and save the bitmap image with a different filename. The server is a Windows 2000 Server.

Question:

What is the best approach to these problems? I’m developing in VB.NET and would like to know if there are HTTP or FTP classes (.NET classes, that is – don’t want to use COM Interop if possible) that I can use to do this. Will a solution a Web Service be advisable? If not, please describe the pros and cons of possible solutions.
0
Comment
Question by:xmlnewbie
  • 4
7 Comments
 
LVL 3

Expert Comment

by:jazduck
ID: 11817214
System.Net contains the classes to download files, and from memory System.Text has streamreaders etc that make working with files fairly easy.

http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_21062318.html
Is an example of a function that downloads a file, and saves to to a particular spot.

Ill try and dig out some code for ftp, and reading/parsing a text file, but im nowhere near my development machine & someone else will probably answer them before i can.

Jaz
0
 
LVL 3

Expert Comment

by:jazduck
ID: 11817227
Sorry. Its System.IO that contains the classes for file manipulation, ill try and find examples later if it hasnt been answered.

Jaz
0
 

Author Comment

by:xmlnewbie
ID: 11857846
Jaz???
0
 
LVL 3

Expert Comment

by:jazduck
ID: 11949315
Ok sorry about the delay, i havn't been able to spend much time doing anything but studying recently.

This should work for http & ftp file transfers.  Dont quote me on the ftp part I havn't had chance to test it.  Paste the following into your vb.net. To upload you will probably have to be authenticated & have the correct permissions. Anyway it should show you how to download or upload files.

Public Class Form1
    Inherits System.Windows.Forms.Form
    Dim WC As New System.Net.WebClient()

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
    Friend WithEvents TextBox4 As System.Windows.Forms.TextBox
    Friend WithEvents TextBox3 As System.Windows.Forms.TextBox
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents Button2 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.TextBox1 = New System.Windows.Forms.TextBox()
        Me.TextBox2 = New System.Windows.Forms.TextBox()
        Me.Button1 = New System.Windows.Forms.Button()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.TextBox4 = New System.Windows.Forms.TextBox()
        Me.TextBox3 = New System.Windows.Forms.TextBox()
        Me.Label2 = New System.Windows.Forms.Label()
        Me.Button2 = New System.Windows.Forms.Button()
        Me.SuspendLayout()
        '
        'TextBox1
        '
        Me.TextBox1.Location = New System.Drawing.Point(8, 24)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.Size = New System.Drawing.Size(360, 20)
        Me.TextBox1.TabIndex = 1
        Me.TextBox1.Text = "http://www.somewebpage.com/filetodownload.txt"
        '
        'TextBox2
        '
        Me.TextBox2.Location = New System.Drawing.Point(8, 56)
        Me.TextBox2.Name = "TextBox2"
        Me.TextBox2.Size = New System.Drawing.Size(360, 20)
        Me.TextBox2.TabIndex = 2
        Me.TextBox2.Text = "C:\somedirectory\filetosaveitas.txt"
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(384, 24)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(64, 56)
        Me.Button1.TabIndex = 3
        Me.Button1.Text = "Download"
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(16, 88)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(424, 40)
        Me.Label1.TabIndex = 4
        Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'TextBox4
        '
        Me.TextBox4.Location = New System.Drawing.Point(8, 168)
        Me.TextBox4.Name = "TextBox4"
        Me.TextBox4.Size = New System.Drawing.Size(360, 20)
        Me.TextBox4.TabIndex = 6
        Me.TextBox4.Text = "C:\somedirectory\filetoupload.txt"
        '
        'TextBox3
        '
        Me.TextBox3.Location = New System.Drawing.Point(8, 136)
        Me.TextBox3.Name = "TextBox3"
        Me.TextBox3.Size = New System.Drawing.Size(360, 20)
        Me.TextBox3.TabIndex = 5
        Me.TextBox3.Text = "http://www.somewebpage.com/uploadedfile.txt"
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(16, 208)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(424, 56)
        Me.Label2.TabIndex = 7
        Me.Label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'Button2
        '
        Me.Button2.Location = New System.Drawing.Point(384, 136)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(64, 56)
        Me.Button2.TabIndex = 8
        Me.Button2.Text = "Upload"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(464, 278)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button2, Me.Label2, Me.TextBox4, Me.TextBox3, Me.Label1, Me.Button1, Me.TextBox2, Me.TextBox1})
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            WC.DownloadFile(TextBox1.Text, TextBox2.Text)
            Label1.Text = "Downloaded."
        Catch ex As Exception
            Label1.Text = ex.Message.ToString
        End Try
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Try
            WC.UploadFile(TextBox3.Text, TextBox4.Text)
            Label2.Text = "Uploaded."
        Catch ex As Exception
            Label2.Text = ex.Message
        End Try
    End Sub
End Class
0
 
LVL 3

Accepted Solution

by:
jazduck earned 125 total points
ID: 11949803
http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21090841.html

See the Accepted Answer for a Function to Read From a File.

Hope it Helps, you have the necessary code now to download & read a file.

Jaz
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now