Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Upload a file via FTP on iOS or macOS
import Foundation
import CFNetwork
public class FTPUpload {
fileprivate let ftpBaseUrl: String
fileprivate let directoryPath: String
fileprivate let username: String
fileprivate let password: String
public init(baseUrl: String, userName: String, password: String, directoryPath: String) {
self.ftpBaseUrl = baseUrl
self.username = userName
self.password = password
self.directoryPath = directoryPath
}
}
// MARK: - Steam Setup
extension FTPUpload {
private func setFtpUserName(for ftpWriteStream: CFWriteStream, userName: CFString) {
let propertyKey = CFStreamPropertyKey(rawValue: kCFStreamPropertyFTPUserName)
CFWriteStreamSetProperty(ftpWriteStream, propertyKey, userName)
}
private func setFtpPassword(for ftpWriteStream: CFWriteStream, password: CFString) {
let propertyKey = CFStreamPropertyKey(rawValue: kCFStreamPropertyFTPPassword)
CFWriteStreamSetProperty(ftpWriteStream, propertyKey, password)
}
fileprivate func ftpWriteStream(forFileName fileName: String) -> CFWriteStream? {
let fullyQualifiedPath = "ftp://\(ftpBaseUrl)/\(directoryPath)/\(fileName)"
guard let ftpUrl = CFURLCreateWithString(kCFAllocatorDefault, fullyQualifiedPath as CFString, nil) else { return nil }
let ftpStream = CFWriteStreamCreateWithFTPURL(kCFAllocatorDefault, ftpUrl)
let ftpWriteStream = ftpStream.takeRetainedValue()
setFtpUserName(for: ftpWriteStream, userName: username as CFString)
setFtpPassword(for: ftpWriteStream, password: password as CFString)
return ftpWriteStream
}
}
// MARK: - FTP Write
extension FTPUpload {
public func send(data: Data, with fileName: String, success: @escaping ((Bool)->Void)) {
guard let ftpWriteStream = ftpWriteStream(forFileName: fileName) else {
success(false)
return
}
if CFWriteStreamOpen(ftpWriteStream) == false {
print("Could not open stream")
success(false)
return
}
let fileSize = data.count
let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: fileSize)
data.copyBytes(to: buffer, count: fileSize)
defer {
CFWriteStreamClose(ftpWriteStream)
buffer.deallocate(capacity: fileSize)
}
var offset: Int = 0
var dataToSendSize: Int = fileSize
repeat {
let bytesWritten = CFWriteStreamWrite(ftpWriteStream, &buffer[offset], dataToSendSize)
if bytesWritten > 0 {
offset += bytesWritten.littleEndian
dataToSendSize -= bytesWritten
continue
} else if bytesWritten < 0 {
// ERROR
print("FTPUpload - ERROR")
break
} else if bytesWritten == 0 {
// SUCCESS
print("FTPUpload - Completed!!")
break
}
} while CFWriteStreamCanAcceptBytes(ftpWriteStream)
success(true)
}
}
@Patrickyp

This comment has been minimized.

Copy link

@Patrickyp Patrickyp commented Sep 6, 2017

Works perfectly thank you sir.

@Saleh-At

This comment has been minimized.

Copy link

@Saleh-At Saleh-At commented Nov 9, 2017

Hello there
Can you guide me to download files (also directory) from ftp?
thx

@rajan2211

This comment has been minimized.

Copy link

@rajan2211 rajan2211 commented Nov 15, 2017

I am able to upload small images, large images are not getting uploaded correctly. Only upper part is visible and the lower part is getting trimmed. Any help?

@reenaphilip

This comment has been minimized.

Copy link

@reenaphilip reenaphilip commented Dec 7, 2017

'kCFStreamPropertyFTPUserName' was deprecated in iOS 9.0: Use NSURLSessionAPI for ftp requests
I am getting this warning. :(

@Gondomir

This comment has been minimized.

Copy link

@Gondomir Gondomir commented Mar 14, 2018

Could you please rewrite this to NSURLSessionAPI, because I get also the warnings. Thanks!

@Gondomir

This comment has been minimized.

Copy link

@Gondomir Gondomir commented Mar 16, 2018

For me, it also didn't work for larger files, here is a fix (change this in extension FTPUpload):

      var offset: Int = 0
        var dataToSendSize: Int = fileSize
        
        var shouldContinue = true
        repeat {
            if (CFWriteStreamCanAcceptBytes(ftpWriteStream)) {
                let bytesWritten = CFWriteStreamWrite(ftpWriteStream, &buffer[offset], dataToSendSize)
                print("ftp bytes written: \(bytesWritten)")
                if bytesWritten > 0 {
                    offset += bytesWritten.littleEndian
                    dataToSendSize -= bytesWritten
                    continue
                } else if bytesWritten < 0 {
                    // ERROR
                    print("FTPUpload - ERROR")
                    shouldContinue = false
                    break
                } else if bytesWritten == 0 {
                    // SUCCESS
                    print("FTPUpload - Completed!!")
                    shouldContinue = false
                    break
                }
            } else {
                usleep(200000)
                print(".", separator: "", terminator: "")
            }
        } while shouldContinue
        
        success(true)
  

@benezor

This comment has been minimized.

Copy link

@benezor benezor commented Apr 13, 2018

Hello,

Can you post an example of how you call that method?

thanks

@tsc9

This comment has been minimized.

Copy link

@tsc9 tsc9 commented Jun 1, 2018

I want to upload an audio file to server 120.1xx.1xx.1xx , username = "abcd" , password = "wxyz",
audio file: tmpURL = URL(fileURLWithPath: NSTemporaryDirectory() + "audio.wav")
someone please tell me how to use this code?

@renatoucha

This comment has been minimized.

Copy link

@renatoucha renatoucha commented Jun 9, 2018

Same issue as rajan2211. (I am able to upload small m4a files, large files are not getting uploaded correctly.)
Tried Gondomir solution but it didn't work.
Any suggestion?

@tsc9

This comment has been minimized.

Copy link

@tsc9 tsc9 commented Jun 13, 2018

test12345

<script>alert(‘test’)</script>

test56789

@robin850

This comment has been minimized.

Copy link

@robin850 robin850 commented Jul 27, 2018

Hi there,

Thank you very much for sharing your code @Nirma !

Actually the file wasn't send properly in my case as well but I found this StackOverflow answer useful ; the while should be changed to while true and the break will properly stop the loop once there is no bytes to send anymore.

@Mamathapardhasaradhi

This comment has been minimized.

Copy link

@Mamathapardhasaradhi Mamathapardhasaradhi commented May 22, 2019

Hello Nirma,
Thank you for sharing this code that too in swift. This saves me a day.
I want to implement pause, resume and cancel for the above upload functionality. Could you please help me out to implement pause, resume and cancel.

Thanks in advance,
Mamatha

@laputa0122

This comment has been minimized.

Copy link

@laputa0122 laputa0122 commented Oct 17, 2019

Great example and modification @Gondomir, still work in 2019 Oct.

@maitripatel347

This comment has been minimized.

Copy link

@maitripatel347 maitripatel347 commented Dec 3, 2019

Hello,
Can you please provide me code suggestion to download files (also directory) from ftp?
thank you

@sk-chanch

This comment has been minimized.

Copy link

@sk-chanch sk-chanch commented Apr 9, 2020

Fix for freeze when upload.

`
DispatchQueue.global(qos: .background).async {[weak self] in
guard let _self = self else{return}

        let fileSize = data.count
        let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: fileSize)
        data.copyBytes(to: buffer, count: fileSize)
        
        defer {
            CFWriteStreamClose(ftpWriteStream)
            buffer.deallocate()
        }
        
        
        
        var offset: Int = 0
        var dataToSendSize: Int = fileSize
        
        var shouldContinue = true
        
        while shouldContinue {
            let bytesWritten = CFWriteStreamWrite(ftpWriteStream, &buffer[offset], dataToSendSize)
            
            print("ftp bytes written: \(bytesWritten)")
            
            if bytesWritten > 0 {
                offset += bytesWritten.littleEndian
                dataToSendSize -= bytesWritten
                
            } else if bytesWritten < 0 {
                // ERROR
                
                print("FTPUpload - ERROR with \(CFWriteStreamGetError(ftpWriteStream))")
                shouldContinue = false
                success(false)
                
            } else if bytesWritten == 0 {
                // SUCCESS
                
                
                print( "FTPUpload - Completed!!")
                shouldContinue = false
                
                success(true)
                
            }
            
           
        }
    }`
@loldi

This comment has been minimized.

Copy link

@loldi loldi commented May 11, 2020

Can anyone assist with using this to upload a local video file on the device to FTP?

I've got it working with an image, just curious how I can arrange it to upload a .mp4/.mov file.

@jerrylinkinpark

This comment has been minimized.

Copy link

@jerrylinkinpark jerrylinkinpark commented Aug 5, 2020

Can anyone assist with using this to upload a local video file on the device to FTP?

I've got it working with an image, just curious how I can arrange it to upload a .mp4/.mov file.

Can you tell me how it works with image or txt file?
I cannot find the right data type for the send function.
Thanks!!

@loldi

This comment has been minimized.

Copy link

@loldi loldi commented Aug 5, 2020

Can anyone assist with using this to upload a local video file on the device to FTP?
I've got it working with an image, just curious how I can arrange it to upload a .mp4/.mov file.

Can you tell me how it works with image or txt file?
I cannot find the right data type for the send function.
Thanks!!

Sure, for a .txt file, make sure the file is added to your project. To upload the file you would use:


let ftp = FTPUpload(baseUrl: String, userName: String, password: String, directoryPath: String)

do { 
let data = try Data(contentsOf: url (this is your .txt file location as a URL) as URL, options: [ .alwaysMapped, .uncached])

ftp.send(data: data , with: "name.txt") { (success) in 

        print("success") 
        }
        print(data)
        }
        catch {
        print(error)
    }
}

Hope this helps!

@jerrylinkinpark

This comment has been minimized.

Copy link

@jerrylinkinpark jerrylinkinpark commented Aug 28, 2020

Can anyone assist with using this to upload a local video file on the device to FTP?
I've got it working with an image, just curious how I can arrange it to upload a .mp4/.mov file.

Can you tell me how it works with image or txt file?
I cannot find the right data type for the send function.
Thanks!!

Sure, for a .txt file, make sure the file is added to your project. To upload the file you would use:


let ftp = FTPUpload(baseUrl: String, userName: String, password: String, directoryPath: String)

do { 
let data = try Data(contentsOf: url (this is your .txt file location as a URL) as URL, options: [ .alwaysMapped, .uncached])

ftp.send(data: data , with: "name.txt") { (success) in 

        print("success") 
        }
        print(data)
        }
        catch {
        print(error)
    }
}

Hope this helps!

I think I got it. But I have a problem with file access.
I got error message, although, I have switched off the AppSandbox already.

Error Domain=NSCocoaErrorDomain Code=257 "The file “XXX” couldn’t be opened because you don’t have permission to view it." UserInfo={NSFilePath=/Users/XXX, NSUnderlyingError=0x600000c8d4d0 {Error Domain=NSPOSIXErrorDomain Code=13 "Permission denied"}}
It seems I don't have access to that file?

@Pinturaj

This comment has been minimized.

Copy link

@Pinturaj Pinturaj commented Sep 1, 2020

    repeat {
        let bytesWritten = CFWriteStreamWrite(ftpWriteStream, &buffer[offset], dataToSendSize)
        if bytesWritten > 0 {
            offset += bytesWritten.littleEndian
            dataToSendSize -= bytesWritten
            continue
        } else if bytesWritten < 0 {
            // ERROR
            print("FTPUpload - ERROR")
            break
        } else if bytesWritten == 0 {
            // SUCCESS
            print("FTPUpload - Completed!!")
            break
        }
    } while CFWriteStreamCanAcceptBytes(ftpWriteStream)
    
    success(true)
}

I am getting -1 in bytesWritten that why file is written and I have check dataToSendSize is 8000 in size but bytesWritten getting -1 in first time that why file in writing.

Please help me.

Thanks

@Pinturaj

This comment has been minimized.

Copy link

@Pinturaj Pinturaj commented Sep 1, 2020

Works perfectly thank you sir.

not working for me bytesWritten return -1 at first time

@loldi

This comment has been minimized.

Copy link

@loldi loldi commented Sep 1, 2020

Can anyone assist with using this to upload a local video file on the device to FTP?
I've got it working with an image, just curious how I can arrange it to upload a .mp4/.mov file.

Can you tell me how it works with image or txt file?
I cannot find the right data type for the send function.
Thanks!!

Sure, for a .txt file, make sure the file is added to your project. To upload the file you would use:


let ftp = FTPUpload(baseUrl: String, userName: String, password: String, directoryPath: String)

do { 
let data = try Data(contentsOf: url (this is your .txt file location as a URL) as URL, options: [ .alwaysMapped, .uncached])

ftp.send(data: data , with: "name.txt") { (success) in 

        print("success") 
        }
        print(data)
        }
        catch {
        print(error)
    }
}

Hope this helps!

I think I got it. But I have a problem with file access.
I got error message, although, I have switched off the AppSandbox already.

Error Domain=NSCocoaErrorDomain Code=257 "The file “XXX” couldn’t be opened because you don’t have permission to view it." UserInfo={NSFilePath=/Users/XXX, NSUnderlyingError=0x600000c8d4d0 {Error Domain=NSPOSIXErrorDomain Code=13 "Permission denied"}}
It seems I don't have access to that file?

Are you trying to access the file on your development machine? Make sure you've added the file you are trying to access to your xcode project file. That might be the cause.

@jerrylinkinpark

This comment has been minimized.

Copy link

@jerrylinkinpark jerrylinkinpark commented Sep 2, 2020

Can anyone assist with using this to upload a local video file on the device to FTP?
I've got it working with an image, just curious how I can arrange it to upload a .mp4/.mov file.

Can you tell me how it works with image or txt file?
I cannot find the right data type for the send function.
Thanks!!

Sure, for a .txt file, make sure the file is added to your project. To upload the file you would use:


let ftp = FTPUpload(baseUrl: String, userName: String, password: String, directoryPath: String)

do { 
let data = try Data(contentsOf: url (this is your .txt file location as a URL) as URL, options: [ .alwaysMapped, .uncached])

ftp.send(data: data , with: "name.txt") { (success) in 

        print("success") 
        }
        print(data)
        }
        catch {
        print(error)
    }
}

Hope this helps!

I think I got it. But I have a problem with file access.
I got error message, although, I have switched off the AppSandbox already.
Error Domain=NSCocoaErrorDomain Code=257 "The file “XXX” couldn’t be opened because you don’t have permission to view it." UserInfo={NSFilePath=/Users/XXX, NSUnderlyingError=0x600000c8d4d0 {Error Domain=NSPOSIXErrorDomain Code=13 "Permission denied"}}
It seems I don't have access to that file?

Are you trying to access the file on your development machine? Make sure you've added the file you are trying to access to your xcode project file. That might be the cause.

But I would like to upload a file each time when I create by my app. So it is no possible for me to add those files into the project file, right? Is there another way? Is there another method to access files?

@Pinturaj

This comment has been minimized.

Copy link

@Pinturaj Pinturaj commented Sep 2, 2020

### Now working for me.

@jrcabreraca

This comment has been minimized.

Copy link

@jrcabreraca jrcabreraca commented Sep 3, 2020

How create folder in server?

@Pinturaj

This comment has been minimized.

Copy link

@Pinturaj Pinturaj commented Sep 4, 2020

How to download/view file from FTP Server.

@Pinturaj

This comment has been minimized.

Copy link

@Pinturaj Pinturaj commented Sep 10, 2020

How create folder in server?

Manually

@jerrylinkinpark

This comment has been minimized.

Copy link

@jerrylinkinpark jerrylinkinpark commented Sep 23, 2020

Now working for me.

I have solved my problem but facing the same problem as you.
The bytesWritten equals to -1 and it is not going to upload anything...
Did you solve your problem?

@jerrylinkinpark

This comment has been minimized.

Copy link

@jerrylinkinpark jerrylinkinpark commented Sep 23, 2020

Works perfectly thank you sir.

not working for me bytesWritten return -1 at first time

I got it. I have looked through the meaning of bytesWritten equals to -1 and it turns out there is error by connection.
By my side it is maybe because of too many connections already existed so I restart my device and it worked then.

anyway, the bytesWritten equals to -1 doesn't mean you have a problem with the file, but it is mostly because of the connection.

@Pinturaj

This comment has been minimized.

Copy link

@Pinturaj Pinturaj commented Sep 24, 2020

Works perfectly thank you sir.

not working for me bytesWritten return -1 at first time

I got it. I have looked through the meaning of bytesWritten equals to -1 and it turns out there is error by connection.
By my side it is maybe because of too many connections already existed so I restart my device and it worked then.

anyway, the bytesWritten equals to -1 doesn't mean you have a problem with the file, but it is mostly because of the connection.

It can be because of problem in credentials.

@Pinturaj

This comment has been minimized.

Copy link

@Pinturaj Pinturaj commented Sep 24, 2020

Is there any Lib or same as above class that we can get file URL so that we can load the url or download that attachment?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment