A New Internet Library: Add Your Website/Blog or Suggest A Website/Blog to our Free Web Directory http://anil.myfunda.net.

Its very simple, free and SEO Friendly.
Submit Now....

Thursday, July 24, 2008

Can I upload a file stored in a variable without first writing it to the file system

Currently I am uploading a file, using FTP, from the file system. it all works fine.

I have the content of the file in a variable before I write it to the file system.

Can I upload the file stored in a variable without first writing it to the file system??

-----------------------------

Function uploadFileUsingFTP(ByVal CompleteFTPPath As String, ByVal CompleteLocalPath As String, Optional ByVal UName As String = "", Optional ByVal PWD As String = "")

        'Create a FTP Request Object and Specfiy a Complete Path
        Dim reqObj As System.Net.FtpWebRequest = System.Net.WebRequest.Create(CompleteFTPPath)

        'Call A FileUpload Method of FTP Request Object
        reqObj.Method = System.Net.WebRequestMethods.Ftp.UploadFile

        'Create, delete, rename files, directories AND MORE
        'reqObj.Method = System.Net.WebRequestMethods.Ftp.DeleteFile

        'If you want to access Resourse Protected You need to give User Name      and PWD
        reqObj.Credentials = New System.Net.NetworkCredential(UName, PWD)


        'FileStream object read file from Local Drive
        Dim streamObj As System.IO.FileStream = System.IO.File.OpenRead(CompleteLocalPath)

        'Store File in Buffer
        Dim buffer(streamObj.Length) As Byte

        'Read File from Buffer
        streamObj.Read(buffer, 0, buffer.Length)

        'Close FileStream Object Set its Value to nothing
        streamObj.Close()
        streamObj = Nothing

        'Upload File to ftp://localHost/ set its object to nothing
        'reqObj.GetRequestStream.Write(buffer, 0, buffer.Length) 'Doesn't close file
        Using content As System.IO.Stream = reqObj.GetRequestStream()
            content.Write(buffer, 0, buffer.Length)
        End Using

        reqObj = Nothing

End Function



Source Click Here.

No comments:

Post a Comment

Post your comments here:

Originals Enjoy