簡體   English   中英

如何監控新上傳文件的FTP傳入文件夾?

[英]How to monitor FTP incoming folder for newly uploaded files?

換句話說,如何使用IIS支持的FTP服務器和.NET啟動我的機器上FTP服務器的新上傳文件的自定義處理?

澄清這是在托管FTP服務器的計算機上運行。 在我的情況下,權限不是問題。

看來我需要讓上傳實體添加一個在完全上傳實際數據文件后發送的列表文件。 想象一下,上傳了一個實際的xxx.data文件,然后是xxx.listing文件,表示xxx.data文件上傳完成。

最初這個問題是關於Reactive Extensions。 我希望有人擁有從IIS 7.5的FTP服務器到Reactive Extensions的橋梁,因此我可以專注於更高級別的實現細節。

Rx 本身並不適合。

當您想要查詢事件(或時間集合)並且想要組合復雜的過濾,連接和投影時,Rx非常適合。

在觀看FTP上傳或文件系統更改方面,Rx絕對沒有任何作用。 或者任何事件來源真的。 沒有提供任何生成這些類型事件的機制。

相反,它確實允許一種方法將各種不同的事件源 - 事件,異步操作,WMI事件,生成的可觀察量等 - 集合到一個通用框架中。

您需要查看類似FileSystemWatcher ,然后將其轉換為可觀察對象。

然后你可以用Rx做一些很棒的事情。 真的很棒。

我已經縮減了MSDN示例以專注於文件創建,因為它是文件上傳到FTP服務器時唯一感興趣的事件。

必須為此代碼示例安裝Reactive Extensions才能在Visual Studio 2012中運行。

 class Program
 {
     static void Main()
     {
         // Create a FileSystemWatcher to watch the FTP incoming directory for creation of listing file

         using (var watcher = new FileSystemWatcher(@"C:\FTP-Data\Incoming", "*.lst"))
         {
             // Use the FromEvent operator to setup a subscription to the Created event.           
             //                                                                                    
             // The first lambda expression performs the conversion of Action<FileSystemEventArgs> 
             // to FileSystemEventHandler. The FileSystemEventHandler just calls the handler       
             // passing the FileSystemEventArgs.                                                   
             //                                                                                    
             // The other lambda expressions add and remove the FileSystemEventHandler to and from 
             // the event.                                                                         

             var fileCreationObservable = Observable.FromEvent<FileSystemEventHandler, FileSystemEventArgs>(
                                         UseOnlyTheSecondArgument,
                                         fsHandler => watcher.Created += fsHandler,
                                         fsHandler => watcher.Created -= fsHandler);

             fileCreationObservable.Subscribe(ActionWhenFileIsUploaded);

             watcher.EnableRaisingEvents = true;

             Console.WriteLine("Press ENTER to quit the program...\n");
             Console.ReadLine();
         }
     }

     private static void ActionWhenFileIsUploaded(FileSystemEventArgs args)
     {
         Console.WriteLine("{0} was created.", args.FullPath);

         // TODO
         // 1. Deduce original file name from the listing file info
         // 2. Consume the data file
         // 3. Remove listing file
     }

     private static FileSystemEventHandler UseOnlyTheSecondArgument(Action<FileSystemEventArgs> handler)
     {
         return (object sender, FileSystemEventArgs e) => handler(e);
     }
 }

我仍在研究將可觀察的列表文件轉換為可觀察的實際數據文件的細節。 敬請關注。

您需要擴展FileWatcher的功能,即檢查您的文件是否已完全上傳(或不完整),然后使用注釋中列出的Rx示例。

要么..

不直接觀察FTP文件夾中的文件..而是運行Windows服務或某些應用程序完全上傳的文件復制處理文件夾 ,然后您確定只有沒有部分文件。 (將需要弄清楚如何。想知道文件是否在上傳服務器期間被鎖定)。 這將在單獨的應用程序中分離觀察和處理邏輯。(但使它更棘手。)。 處理完畢后,您也可以將文件移出處理文件夾,這樣就不會再次處理同一個文件(或者您可以使用其他一些邏輯。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM