Upload Image/Video to FTP Swift 3.0

2019-08-18 01:09发布

问题:

I am using below code to upload Image/Video to FTP. The complete image is not getting uploaded. Lowe part of the image is getting cut. At the end of the upload process, the complete data is said to be uploaded but the image is getting cut. I am able to upload small images and videos. The issue is for large images and videos. Kindly let me know the changes I need to do in the below code.

    let buf     = movieData?.bytes.assumingMemoryBound(to: UInt8.self)
    let buf2    = movieData?.bytes.assumingMemoryBound(to: UInt8.self)
    let buf3    = UnsafeMutablePointer<UInt8>.allocate(capacity: (movieData?.length)!)

    var leftOverSize        = movieData?.length
    let bytesFile           = movieData?.length
    var totalBytesWritten   = 0
    var bytesWritten        = 0


    let ftpUrl = NSURL(string: "FTP_URL")
    let stream      = CFWriteStreamCreateWithFTPURL(nil,ftpUrl!).takeUnretainedValue()
    let cfstatus    = CFWriteStreamOpen(stream) as Bool
    // connection fail
    if !cfstatus {
        print("Not connected")
    }

    repeat{
        // Write the data to the write stream
        bytesWritten = CFWriteStreamWrite(stream, buf, leftOverSize!)
        print("bytesWritten: \(bytesWritten)")
        if (bytesWritten > 0) {
            totalBytesWritten += bytesWritten
            // Store leftover data until kCFStreamEventCanAcceptBytes event occurs again
            if (bytesWritten < bytesFile!) {
                leftOverSize = bytesFile! - totalBytesWritten
                memmove(buf3, buf2! + bytesWritten, leftOverSize!)
            }else{
                leftOverSize = 0
            }

        }else{
            print("CFWriteStreamWrite returned \(bytesWritten)")
            break
        }

        if !CFWriteStreamCanAcceptBytes(stream){
            sleep(1)
        }
    }while((totalBytesWritten < bytesFile!))

    CFWriteStreamClose(stream)

回答1:

try LxFTPRequest Framework to upload a file to FTP server

Save Data locally

extension Data {

    func save(withName name: String) -> String{
        let fileManager = FileManager.default
        let paths = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString).appendingPathComponent(name)
        print(paths)
        fileManager.createFile(atPath: paths as String, contents: self, attributes: nil)
        return paths
    }
}

FTP Request

    guard let data = UIImageJPEGRepresentation(image, 0.5) else {
        return
    }

    let filePath = data.save(withName: yourfileName)
    let uploadUrl = URL(string: ftpBaseUrl + imageName)

    request = LxFTPRequest.upload()
    request.serverURL = uploadUrl
    request.localFileURL = URL(fileURLWithPath: filePath)
    request.username = username
    request.password = password

    request.successAction = { (resultClass, result) in
        // success ...
    }

    request.failAction = { (domain, error, errorMessage) in
        //Fail ....
    }

    request.progressAction = {(_ totalSize: Int, _ finishedSize: Int, _ finishedPercent: CGFloat) -> Void in

    }

    request.start()


标签: ios swift ftp