Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 164
  • Last Modified:

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

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
xmlnewbie
Asked:
xmlnewbie
  • 4
1 Solution
 
jazduckCommented:
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
 
jazduckCommented:
Sorry. Its System.IO that contains the classes for file manipulation, ill try and find examples later if it hasnt been answered.

Jaz
0
 
xmlnewbieAuthor Commented:
Jaz???
0
 
jazduckCommented:
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
 
jazduckCommented:
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now