How to Upload Zip Data with Alamofire

How to upload zip data with alamofire?

Eventually, I found the issue, server side does not accept my request. Also, there is some confusion, because if I try to download an image file from my project it works, but if the file is selected from the document directory then there is an issue.

Anyway if someone has a similar issue try to check your server side.

Try to check if your request came to server and with content inside.

Download & Upload zip file

The following code works:

        let data = try? NSData(contentsOf: URL(string:str)!) as Data

AF.upload(multipartFormData: { multipartFormData in
multipartFormData.append(data!,withName: "file",fileName: file_name ,mimeType: "application/octet-stream")
}, to: "http://"+dev.ipAddress+"/update")
.response { response in
if response.response?.statusCode != 200 {
devicesUpdateError.append(dev.name)
}
}

Swift How to Unzip file from URL with Alamofire and SSZipArchive

By looking at your code, the path you are using for the source is perfect.

For the destination path, try updating it to

func unzipPath(fileName:String) -> String? {
let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString
let pathWithComponent = path.appendingPathComponent("test\(fileName)")
do {
try FileManager.default.createDirectory(atPath: pathWithComponent, withIntermediateDirectories: true, attributes: nil)
} catch {
return nil
}
return pathWithComponent
}

Try and share the results.



Related Topics



Leave a reply



Submit