Understand ftp with asp.net in 5 Minutes Part- IInd

Hello Friends,

 In last article we have did how to upload a file I know you are very eager to know how to download a file.  I am not taking much time here and will introduce you below code by which you can download a file. It just likes our earlier upload ftp file.Its screen is just like below.

  DownloadFTP

On the button click we did following thing

  Protected Sub btnDownload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDownload.Click

        Try

            Dim ftpRes As FtpWebRequest = DirectCast(FtpWebRequest.Create(Me.txtFtp.Text & “/” & Me.txtRemoteFileName.Text), FtpWebRequest)

            ftpRes.Credentials = New NetworkCredential(Me.txtUserName.Text, Me.txtPassword.Text)

            ftpRes = DirectCast(WebRequest.Create(Me.txtFtp.Text & “/” & Me.txtRemoteFileName.Text), FtpWebRequest)

            ftpRes.Credentials = New NetworkCredential(Me.txtUserName.Text, Me.txtPassword.Text)

            ftpRes.Method = WebRequestMethods.Ftp.DownloadFile

            ftpRes.UseBinary = True

            Dim myFtpWebResponse As FtpWebResponse = ftpRes.GetResponse()

            Dim myStreamWriter As StreamWriter

            myStreamWriter = New StreamWriter(“c:\Downloads\” & Me.txtRemoteFileName.Text)

            myStreamWriter.Write(New StreamReader(myFtpWebResponse.GetResponseStream()).ReadToEnd)

            myStreamWriter.Close()

            myFtpWebResponse.Close()

        Catch eh As System.Net.WebException

            Response.Write(eh.Message)

        Catch ex As Exception

            Throw ex

        End Try

    End Sub

Here  The main important thing is method which is download file as you see in above code.

We just take response of ftp and read the stream of this with the help of GetResponseStream().ReadToEnd.

Rest all the thing is same like our old code for file upload.

I hope you got with the above code. 

Thanks & Enjoy coding

Your host

Rajat Jaiswal

Understand FTP with asp.net in 5 minutes part-Ist

Hello Friends,
Today I am going to share you new thing in asp.net 2.0 which is “FTP object”. Earlier in dot net 1.1 versions there is many problem to handle “FTP”, means if you want to upload or download a file then you have to do socket programming.
Which I don’t like really but after 2.0 this problem is resolved dot net provide great solution which allow us to connect with ftp and do operation.
I will explain you with my example. So today we took how to upload a file on ftp.
For this I have design following page.

FTPUpload
On this page there is 3 text box & file upload control basically which is
txtFtp – which keep ftp url like ftp://myftp.com
txtUserName – which keep ftp username for credential
txtPassword – which keep ftp password for credential
ctrlFlup – which keep path from where we have to upload file.

There is a button which is btnUpload on page. On click of this page we are going to upload the selected file.
So in code behind our first and most important statement is import J
Just see below code
Imports System.Net
Imports System
Imports System.Net
Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpload.Click
Try
Dim ftpRes As FtpWebRequest = DirectCast(FtpWebRequest.Create(Me.txtFtp.Text & “/” & Me.ctrlflUp.FileName), FtpWebRequest)
ftpRes.Credentials = New NetworkCredential(Me.txtUserName.Text, Me.txtPassword.Text)
ftpRes.Method = System.Net.WebRequestMethods.Ftp.UploadFile
Dim b As Byte() = Me.ctrlflUp.FileBytes
Dim fstream As System.IO.Stream = ftpRes.GetRequestStream()
fstream.Write(b, 0, b.Length)
fstream.Close()
fstream.Dispose()
Dim myresponse As FtpWebResponse = DirectCast(ftpRes.GetResponse, FtpWebResponse)
Response.Write(myresponse.StatusDescription)
Catch eh As System.Net.WebException
Response.Write(eh.Message)
Catch ex As Exception
Throw ex
End Try
End Sub
End Class

Here I will explain you the ftpwebrequest is the object which interact with FTP.
So when button upload is click we first create a object of ftp web request.
Once the object is created its credential is required for which we create a new network credential and just pass username & password.
After this you will find another important statement which is Web Request method.
Basically its notify that what user want to do import file, upload file, change directory etc.
In Next step what ever the file which we want to upload just convert that in byte array.
By file upload object as shown below
Dim b as byte() = me.ctrlflUp.fileBytes()
Once it done we just create a stream to write file on ftp.
So we just create a IO stream and write file on ftp.
To get response from Ftp as you know FTP has special command and status so we use ftp web response object.
If there is proper status then we can say our file is uploaded successfully.
In next Session we are going to download file from ftp with the help of this object.
Till then enjoy programming
Enjoy ftp.

Your friend
Rajat Jaiswal

How to read Resource files in our code by LINQ to XML ?

Hello friends,

Sorry for such a late post. Currently I am busy in too many works so not getting enough time to talk with you.

Today I am posting an interesting work which is how to read a resource file in our code with the help of LINQ TO SQL.

I can say only one thing which is its simple and easy to use. Just copy paste below code and you will get what to want.

Before code I will explain you that resource file is just like xml which has attributes and value and that particular attributes is your key.

 

Now we need to fetch value for particular key for a file for this you have to write below code. Where bv_strFileName is your resource file name and bv_strKey is key for which you need value.

Public Shared Function pub_LangaugeValue(ByVal bv_strFile As String, ByVal bv_strKey As String) As String

Try

Dim strValue As String = “”

Dim resxXML As New XDocument

resxXML = XDocument.Load(bv_strFile)

Dim a = From data In resxXML.Root.Descendants(“data”) _

Where data.Attribute(“name”).Value = bv_strKey _

Select New With {.resxA = data.Attribute(“name”).Value, .ResxValue = data.Element(“value”).Value}

If a.Count() > 0 Then

strValue = a.FirstOrDefault().ResxValue.ToString()

Else

strValue = bv_strKey

End If

Return strValue

Catch ex As Exception

Throw ex

End Try

End Function ‘pub_LangaugeValue

 

Where bv_strFileName is full path name of our resource file and bv_strkey is the key for which we need to find the value.

Below is example how to use it our aspx.

Suppose we want to read LabelResource1.text value in Spanish whose file name is home.aspx.es.resx.

 

Then we have to call the above function like below.

Response.write(pub_LanguageValue(server.mapPath(“/App_LocalResources/home.aspx.es.resx”),”LabelResource1.text”)

With the help of above code.

We can get key’s value from different resource file.

 

Thanks

Rajat Jaiswal