Month: <span>April 2009</span>

A while back I wrote an ASP.NET user control that FTPs files to a server. It has a FileUpload control that allows the user to select the file they want to transfer, plus it uses some AJAX stuff to display a running total of bytes that have been sent. And it works great. I can throw it on an aspx page and upload to my heart’s content. But if I load it inside a custom web part within SharePoint, it tragically fails 100% of the time.

My control uses a third-party FTP library that supports asynchronous uploads via a BeginUpload method. That’s what I’m using in order to periodically refresh part of the page with the byte count. But for reasons unknown, when this code executes inside the SharePoint environment, the file stream from the FileUpload control gets unexpectedly closed after 64 KB have been transferred. As expected, the FTP library reacts badly when this happens. The crazy part is I could get it to work in SharePoint if I removed any one piece of the equation: if I used the synchronous upload method of the FTP library instead of the async one, or if I opened another thread but did anything else in it except try to FTP a file (counting to 20 on the page worked fine). I originally had the FileUpload control inside an UpdatePanel, so I took it out. Same result. (I know that FileUpload isn’t strictly supported inside update panels, but I had the appropriate trigger set and it was working beforehand anyhow.)

My assumption is that SharePoint was disposing the FileUpload control even though it should have been available during the subsequent async postbacks. Maybe it’s all part of the incompatibility between FileUpload and AJAX. In any case, the solution was to simply save the file on the web server via Request.Files(0).SaveAs, then FTP it. I wasn’t happy about inducing an additional delay, but in testing it wasn’t terrible.  I also save the relevant properties of the FileUpload control, like the file path, content type and content length, before I start the FTP transfer so I don’t have to touch the control at all while the transfer is happening.

So far it’s working swimmingly, but SharePoint is not endearing itself to my heart.