Forum Discussion
RobbSalzmann
12 months agoValued Contributor II
Business rules run in the context of the server, so it’s tricky to “download” files. Usually files are put in a user’s application folder where they can download the file themselves.
If you set up ftp on the machine where you want to put the file, you can use something like this to get it there:
Imports System.Net
Imports System.IO
Public Sub UploadFileToFTP()
' Set the FTP server address and credentials
Dim ftpServer As String = "ftp://yourftpserver.com/ftp_downloads/"
Dim username As String = "yourUsername"
Dim password As String = "yourPassword"
' File to be uploaded
Dim filePath As String = "C:\path\to\your\file.txt"
Dim fileName As String = Path.GetFileName(filePath)
Dim ftpFullpath As String = ftpServer + fileName
Try
Dim ftpRequest As FtpWebRequest = DirectCast(WebRequest.Create(ftpFullpath), FtpWebRequest)
ftpRequest.Credentials = New NetworkCredential(username, password)
ftpRequest.Method = WebRequestMethods.Ftp.UploadFile
ftpRequest.UseBinary = True
ftpRequest.KeepAlive = False
' Read the file data into a byte array
Dim fileContents As Byte()
Using sr As New StreamReader(filePath)
fileContents = Encoding.UTF8.GetBytes(sr.ReadToEnd())
End Using
ftpRequest.ContentLength = fileContents.Length
' Write the file data to the request stream
Using requestStream As Stream = ftpRequest.GetRequestStream()
requestStream.Write(fileContents, 0, fileContents.Length)
End Using
' Get the response from the FTP server
Using response As FtpWebResponse = DirectCast(ftpRequest.GetResponse(), FtpWebResponse)
End Using
Catch ex As Exception
Console.WriteLine(ex.ToString())
End Try
End Sub
Related Content
- 5 months ago
- 11 months ago
- 2 years ago