简体   繁体   English

如何直接从FTP上传csv文件,该文件将作为c#中的输入?

[英]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. 目前,我正在从本地计算机上传csv文件作为输入到我的方法中,该方法会将数据插入sql表中。

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 现在我尝试使用以下代码从ftp位置自动上传文件

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. 并非100%清楚,我已经正确理解了这一点,但是您可以直接从Controller Action中调用DownloadFile方法。 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. 您需要考虑FTP详细信息是否经过硬编码或可更改。 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. 是否要在操作周围合并安全性以及是否每次都将文件称为同一事物,非常容易将其参数化,但是我认为您可能需要加强FTP服务器以获取那里的内容,而不是知道名称。 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. 如果您可以进一步澄清您的问题,我可以尝试并更加具体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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