iOS - Download zip file from URL and upload it to a different URL

Hi, I have a zip file stored on server.

  1. I would like to tmp download it and store it.
  2. I would like to do a POST request and send the file to a different URL (IoT device - REST)

I also tried : URLSession.shared.uploadTask(with: request, fromFile: file_url) without storing the file with no success

func updateDevice(urlStr : String, data_file_url : URL, userCompletionHandler: @escaping (URLResponse?, Error?) -> Void) {

    // Create URL

    let url = URL(string: urlStr)

    guard let requestUrl = url else { fatalError() }

  
    // Create URL Request

    var request = URLRequest(url: requestUrl)

    

    // Specify HTTP Method to use

    request.httpMethod = "POST"

    

    request.setValue("multipart/form-data", forHTTPHeaderField: "Content-Type")

        

    // Send HTTP Request

    let task = URLSession.shared.uploadTask(with: request, fromFile: data_file_url) { (data, response, error) in

        

        // Check if Error took place

        if let error = error {

            userCompletionHandler(nil, error)

        }

        else{

            print("yes")

            userCompletionHandler(response,nil)

        }

    }

    task.resume()

}
func downloadFile(url_download : String, userCompletionHandler: @escaping (URL?, Error?) -> Void) {



    // Create URL

    let url = URL(string: url_download)

    guard let requestUrl = url else { fatalError() }



    // Create URL Request

    let request = URLRequest(url: requestUrl)



    let downloadTask = URLSession.shared.downloadTask(with: request) {

        urlOrNil, responseOrNil, errorOrNil in

        // check for and handle errors:

        // * errorOrNil should be nil

        // * responseOrNil should be an HTTPURLResponse with statusCode in 200..<299



        guard let fileURL = urlOrNil else { return }

        do {

            let documentsURL = try

                FileManager.default.url(for: .documentDirectory,

                                        in: .userDomainMask,

                                        appropriateFor: nil,

                                        create: false)

            let savedURL = documentsURL.appendingPathComponent(fileURL.lastPathComponent)

            try FileManager.default.moveItem(at: fileURL, to: savedURL)

            userCompletionHandler(savedURL,nil)

        } catch {

            userCompletionHandler(nil,error)

        }

    }

    downloadTask.resume()

}
downloadFile(url_download: "https://rojer.me/files/shelly/shelly-homekit-Shelly1PM.zip", userCompletionHandler: { (data, error) in

                    if let data=data {

                        print(data)

                        updateDevice(urlStr: "http://"+dev.ipAddress+"/update", data_file_url: data, userCompletionHandler: { res, error in

                        if let res = res {

                            print(dev.name + " successfully updated")

                            print(res)

                        }

                        else{

                            print("failed to update")

                            print(error!)

                        }

                        })

                    }



                })

A few things to checkout here while debugging this.

  1. Double check that the downloaded file is actually getting to the location on disk that you think it is. As an example your could sanity check this with the Data APIs to load and write the file to the desired location.

  2. I would decouple your upload routine out of the completion block of your download routine just to make it easier on yourself. Create a controlling class that instructs a Network Manager class to either download or upload a file. You could use a delegate to communicate back and forth.

  3. The URLRequest on your upload method looks very sparse. Create a testbed project with a blank iOS app that has a known file and try to iron this out on the client side. If all looks good on your client side, make sure your server supports these actions.

Matt Eaton
DTS Engineering, CoreOS
meaton3@apple.com
iOS - Download zip file from URL and upload it to a different URL
 
 
Q