简体   繁体   中英

How do I upload csv file from FTP directly which will be as input in c#?

Currently i'm uploading csv file from local machine as input to my method which will insert data into sql table.

I was doing this like

[system.web.mvc.httppost]
public actionresult Uploadfile(HttpPostedFileBase file)
{
//}

now i tried to upload file from ftp location automatically using following code

but how can i integrate this with my old above method any guess?

string _ftpURL = "testftp.com";             //Host URL or address of the FTP server
string _UserName = "admin";                 //User Name of the FTP server
string _Password = "admin123";              //Password of the FTP server
string _ftpDirectory = "Receipts";          //The directory in FTP server where the files are present
string _FileName = "test1.csv";             //File name, which one will be downloaded
string _LocalDirectory = "D:\\FilePuller";  //Local directory where the files will be downloaded
DownloadFile(_ftpURL, _UserName, _Password, _ftpDirectory, _FileName, _LocalDirectory);

public void DownloadFile(string ftpURL, string UserName, string Password, string ftpDirectory, string FileName, string LocalDirectory)
{
    if (!File.Exists(LocalDirectory + "/" + FileName))
    {
        try
        {
            FtpWebRequest requestFileDownload = (FtpWebRequest)WebRequest.Create(ftpURL + "/" + ftpDirectory + "/" + FileName);
            requestFileDownload.Credentials = new NetworkCredential(UserName, Password);
            requestFileDownload.Method = WebRequestMethods.Ftp.DownloadFile;
            FtpWebResponse responseFileDownload = (FtpWebResponse)requestFileDownload.GetResponse();
            Stream responseStream = responseFileDownload.GetResponseStream();
            FileStream writeStream = new FileStream(LocalDirectory + "/" + FileName, FileMode.Create);
            int Length = 2048;
            Byte[] buffer = new Byte[Length];
            int bytesRead = responseStream.Read(buffer, 0, Length);
            while (bytesRead > 0)
            {
                writeStream.Write(buffer, 0, bytesRead);
                bytesRead = responseStream.Read(buffer, 0, Length);
            }
            responseStream.Close();
            writeStream.Close();
            requestFileDownload = null;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}

Not 100% clear I have understood this correctly, but you can simply call the DownloadFile method from your Controller Action. You haven't mentioned the namespace/class in here so would need something like

FTPHelper FTP = new FTPHelper();
FTP.DownloadloadFile("TestFTP.com etc etc etc......);

You need to consider if the FTP details are hardcoded or changeable. Whether you want to incorporate security around the action and also if the file is called the same thing each time, very easy to parameterise this but I would have thought you might need to enerumate the FTP Server to pick up what was there rather than knowing the name. There is also no automation here, something still needs to call the action to initiate it.

If you can clarify your question a little more, I can try and be more specific.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM