我使用下面的代码将图像/视频上传到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
// 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)
答案 0 :(得分:0)
尝试LxFTPRequest框架将文件上传到FTP服务器
在本地保存Data
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请求
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()