How to correctly write dynamic files to an FTP server? - c#

I'm using C# and i have written a locally installed application that dynamically generates files which need to be on an FTP server.
Do i generate them to disk then upload them to the FTP server? or is there a way to open a stream to an FTP server and write the files directly?

Check the code sample I gave in this answer, doesn't rely on writing to files. It's not SQL specific and was just a suggestion on how to use SQL CLR integration assemblies to upload output from sql queries to an FTP server. The for loop in the method is just to demonstrate writing to the FTP stream. You should be able to rework to you needs:
How to write stored procedure output directly to a file on an FTP without using local or temp files?

You should follow the class:
System.Net.FtpWebRequest
You will see that its arguments are streams and you can send data to them from any source.
When seaching for .Net capabilities you should be aware of the object browser for visual studio acessible in:
View > other windows > object browser
Is supplies a search over all known assembly .Net objects.

The better way is to save file locally, and upload it later, since there could be problems with upload process.

Since you are using c# I'm thinking maybe you are in a Windows Env. Something I know little about :)
If you are dealing with a unix env, you could just pipe your output thru SSH, which would also take care of encryption overhead.

Related

How to FTP a file from windows to mainframe using C# code?

I want to FTP a file from windows path to mainframe path dev region(host-d2). Im not aware of mainframe,
so let me know how can i upload a file to mainframe in particular region like host-d2 using C# code.
input parameters wil be
input_file path--> where input file is stored.
FTP_Host -->host-d2
useriD-->UserID
password-->password
here there will not be any ftppath, as the region is the only that will be considered in mainframe.
enter code here
In this answer I'm assuming this particular mainframe is running the z/OS operating system, but please correct me if my assumption is incorrect.
If you can use FTP to upload a file from your C# program to any system, then it's the same with z/OS. Obviously z/OS's FTP server needs to be running, suitably configured, and network reachable. For security reasons you should use FTPS (FTP with TLS encryption, applied to both the control and data channels), and the ID and password you use should not be hardcoded and not stored "in the clear." And that's just really the bare minimum when it comes to security. Generally you'll want to transfer the file in binary mode, but ASCII (text) mode can be used if you want codepage conversion, in which case you should make sure you ask z/OS to apply your desired codepage conversion.
If you're uploading the file straight into z/OS zFS (UNIX file system) then that should be quite familiar, with UNIX/Linux-style directories and filenames. z/OS also supports z/OS Data Sets, which introduces concepts such as "records" and "record lengths." These are additional features in z/OS, but it's possible to send FTP commands to inform z/OS's FTP server how you'd like your file upload to be stored as a data set, in particular what the record length should be. Also, data set "directory" names are specified a little differently in the CD command than UNIX/Linux-style (and z/OS zFS) directory names are.
IBM provides ample documentation on the z/OS FTP Server including information on the commands it supports, but one basic approach if you're having trouble is to try to upload a file from a command line FTP client and check to see if you get your desired result on z/OS. If you don't get the desired result, refer to IBM's documentation or another tutorial, and try again.
There are many, many other ways to upload files to z/OS as well, if you prefer something else for whatever reason(s). Here are some examples, in no particular order:
You can mount a directory with a NFS client connected to z/OS's NFS server then simply copy the file to that mounted directory.
You can use z/OS's OpenSSH and SFTP.
You can use managed file transfer products such as IBM MQ Advanced (MQ Managed File Transfer) or Sterling Connect:Direct.
You can upload a file via HTTPS to the IBM HTTP Server for z/OS, included with z/OS.
You can PUT a file via the z/OS Management Facility's REST APIs for working with z/OS files. z/OSMF is also included with z/OS.
You can e-mail the file to z/OS, for z/OS to do something with it. There are at least a couple ways to do that, but one way is to use CASI Software's Mail2ZOS product.
You can not send a file at all, an inherently batch-oriented interaction. Instead, you can stream data in some fashion, for a more real-time (or near real-time) interaction. There are a variety of ways to do that; ask if you want more info.

How to verify that an SCP file upload was successful

I've been given an assignment to use SCP to upload a file to a Secure FTP Server (I'm doing this using Workflow Foundation activities). I can upload the file OK, but I need to do some sort of validation to make sure that the file is, in fact, there.
Any ideas ?
Thanks,
Chew
I've solved this type of problem before by:
uploading the file to the server.
downloading it as a different file name.
running a local comparison of the two files.
A script to do this is relatively easy to set up.

Can I remotely run an ACCESS query on a DB located on an FTP server?

I want to run a sql query on ms-access database that is located at ftp server and get only the result back of that query in my application written in c#. Is it possible?
No. JET must be able to open the file via a standard SMB or local location.
See if you can map the FTP location to an SMB type share using some tool first perhaps. Or copy the file locally.
Edit: A possible workaround (based on #baconsah's answer)
You could actually improve the #baconsah's design, by writing a file to the FTP server. Then have a process on the remote server that picks up the write and which then does the query at that point and making the the results available on the FTP output. You lose the latency but increase the complexity.
A horrible alternative is having an access database run the query on the remote system, on a timed job basis, and output the results to a file in that ftp directory.
You could use the FTP command quote or literal to execute a program (or VBS script) on the FTP server machine.
Your "solution" could work as follows:
Transfer the SQL command text as file to the FTP server
Run the remote command which executes the SQL command on the ms-access DB
Transfer the result of step 2. back to your local machine
As mentioned before, this szenario is unreliable, slow, unsecure, needs error handling, ...

How to map an ftp share folder to a local drive using C#?

How do I map an ftp share folder to a local drive using C#? Is there any class library available for this?
I need to achieve the same functionality as NetDrive(http://www.netdrive.net/) offers using FTP ?
Maybe you can leverage someone else's work and get a headstart. The makers of NetDrive for instance offer an SDK - not sure what that requires / costs, however. But it might be worth an inquiry, no?
And maybe, if you combine this approach to define a remote FTP site as a Windows network share, and then use this code in C# to mount a network share as a drive, you might get your job done :-)
It's a very tall order - if you actually want a local drive, e.g. X:\, to access an FTP site, you'd surely need to write a driver. Not an easy task.
If you want it to simply appear in Windows Explorer somewhere - you can use the shell extension as Marco's answer suggests. But don't expect to be able to treat it like a regular drive.
To create a virtual drive or a folder on existing drive and expose remote FTP server contents that way you need a filesystem driver. Or use can use our Callback File System (CBFS) product which lets you write the code in user-mode and includes a pre-created filesystem driver. CBFS includes a sample, SFTPDisk, that does exactly what you need, but with SFTP protocol (SFTP is not FTP but SSH File Transfer Protocol).
Note, that in FTP there's no function to upload a block to the middle of the existing file. This makes some file write operations trickier than with SFTP or local filesystem - you may need to cache the whole file and upload it asynchronously when it's closed by the client.
There's a project in C# to use an FTP folder as virtual drive -> http://amalgam.codeplex.com/ (it uses dokan).
There's a freeware program that does the same thing -> http://www.ferrobackup.com/ftpuse/

File Transfer using FTP

I need to transfer a big file using FTP in my Windows application to remote machine.
I used command prompt to transfer files using FTP. But how can I do it programmatically
in .net 2.0. Sample program is advantage.
For this you could use: FtpWebRequest.
Here is a great example. I hate linking to another site, but it has an example with it for uploading and downloading using FTP.
http://www.c-sharpcorner.com/uploadfile/neo_matrix/simpleftp01172007082222am/simpleftp.aspx

Categories