Is it possible to use NSUrlSession to upload files to an FTP server?

TrevorYokumTrevorYokum USMember ✭✭
edited October 31 in Xamarin.iOS

Hey everyone, I've been scouring the web for a few hours looking for a solution to a problem that I need to resolve with a Xamarin.iOS app that I've been developing. Most, if not all, of our customers in the field need to upload pictures that they attach to work orders (tickets), which go to an FTP on our backend. My coworker developed the Android app and from him I took the current method, based off of an FtpRequest casted to an FtpWebRequest. It works 90% of the time and uploads to the server reasonably quick if the user has a decent connection.

The problem? I need 100% of the time, and I need it to be in the background. I found that NSUrlSession seems to be my best bet for solving this issue, as it's native to iOS and has support for backgrounding. My only issue is that when I try to create the UploadTask with session.CreateUploadTask, it says "cannot read file at (path)" where path is: file://var/mobile/Containers/Data/Application/850CB1FE-9C2D-456C-8B5F-921DC8D5CEF5/Documents/PolarisPictures2/VUPSXOUTA722002799CMC5022017103109544681088_1.jpeg. I've already confirmed that the file does exist on that path via printing out the file name after foreach( var file in Directory.EnumerateFiles(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "PolarisPictures2") ) Without using the file:// protocol, the error returns with ...is not a valid file:// url. So is it that I'm not accessing the file properly, or is it that there's simply no way to do what I'm trying to do?

And if there is no way, could anyone provide a solution that would best achieve the backgrounding capabilities that I require?

Appreciate it, guys.

Thanks.

EDIT: Here's my code, exactly what I'm trying to do:

       `NSUrlSession session = null;
        NSUrlSessionConfiguration config = 
        NSUrlSessionConfiguration.CreateBackgroundSessionConfiguration("com.PolarisFTPUpload.BackgroundSession");
        session = NSUrlSession.FromConfiguration(config, (NSUrlSessionDelegate)new MySessionDelegate(), new NSOperationQueue());
        string docs = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        docs = docs.Replace("/var", "file://var");
        string filepath = System.IO.Path.Combine(docs, "PolarisPictures2/VUPSXOUTA722002799CMC5022017103109544681088_1.jpeg");
        Console.WriteLine(filepath);
        string UploadURLString = "ftp://myftpUser:myftpPass@myftpIP/Pictures/Database" + "/" + DateTime.Today.Year + "/" + DateTime.Today.Month + 
        "/" + DateTime.Today.Day + "/";
        NSUrlSessionUploadTask uploadTask;
        NSUrl uploadURL = NSUrl.FromString(UploadURLString);
        NSUrlRequest req = NSUrlRequest.FromUrl(uploadURL);
        uploadTask = session.CreateUploadTask(req, NSUrl.FromString(filepath));`
Sign In or Register to comment.