Business rule to download file

ResmiKR
New Contributor II

Dear Community ,

Is it possible to use a business rule to download a file from the file explorer to the local system?

Thank You

4 REPLIES 4

RobbSalzmann
Valued Contributor

 

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

 

JackLacava
Community Manager
Community Manager

Sorry for being that guy, but why would you need that? Users can download files from the File Explorer on their own, which they will have to do - since you cannot know in advance where, on their local machine, they'll be able to save it...

If you want to have a button in a dashboard that pushes a file down to the user, try the Navigation action, it has an option to navigate to a file.

If you want something to run in the background to export stuff, save the output to the FileSystem Share which is on the server - that's easy to do with a rule.

RobbSalzmann
Valued Contributor

All good. 🙂  My assumption was the question specified a BR because the dashboard approach wasn't an option.  Together I think we've covered the main set of options here. 

db_pdx
Contributor III

Is this for end users to download files or more for system administration / data ETL purposes.

If the former, yeah. This is a super common action we've built into our dashboards.  For example: users are looking at a Grid/Pivot of transactional data that corresponds to the intersection they just drilled into.  We have a button that says "Export Data".  The button does two things:

1) Executes a Selection Changed Server Task, which is a BR that exports the data they see on screen as a CSV to the users temp folder with a common name, and then

2) The button immediately does the Selection Changed Navigation Action - Open File, which opens that file in their local Excel (which effectively downloads it)

The key for these types of actions is to consistently store the files.  In the above scenario we use the users temp folder. But you could just as easily tailor this to common Application Public folders.

I'm not as familiar with the system-administration-type, file transfers but it looks like others have suggestions here.