Solved

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

Posted on 2004-08-16
7
154 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

895 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

12 Experts available now in Live!

Get 1:1 Help Now