本文介绍了上传图像/视频到FTP Swift 3.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的代码将图像/视频上传到FTP。完整的图像没有上传。 Lowe图像的一部分正在被切断。在上传过程结束时,整个数据被称为上传,但图像正在被切断。我可以上传小图片和视频。这个问题是针对大图片和视频。请让我知道我在下面的代码中需要做的更改。

  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
/ /连接失败
if!cfstatus {
print(未连接)
}

重复{
//将数据写入写入流
bytesWritten = CFWriteStreamWrite(stream,buf,leftOverSize!)
print( bytesWritten:\(bytesWritten))
if(bytesWritten> 0){
totalBytesWritten + = bytesWritten
//存储剩余数据,直到再次发生kCFStreamEventCanAcceptBytes事件
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)$ b $ ((totalBytesWritten< bytesFile!))

CFWriteStreamClose(stream)


解决方案

尝试框架上传文件到FTP服务器

本地保存数据

 扩展数据{

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请求

  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 =密码

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

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

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


$ b request.start()


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)
解决方案

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()

这篇关于上传图像/视频到FTP Swift 3.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 22:48