0

I try to upload data using Alamofire

Issue is: if I try to upload image from project it works ok, but if I try to upload zip dir I got error and timeout exception

There is my code which produces timeout exception

let configuration = URLSessionConfiguration.default
    configuration.timeoutIntervalForRequest = 10 // seconds
    configuration.timeoutIntervalForResource = 10
    alamoFireManager = Alamofire.SessionManager(configuration: configuration)

let fileData = FileManager.default.contents(atPath: filePath)
alamoFireManager.upload(fileData,
                        to: url,
                        method: .post,
                        headers: headers)
        .validate()
        .responseJSON {}

And here is code which works fine

let configuration = URLSessionConfiguration.default
    configuration.timeoutIntervalForRequest = 10 // seconds
    configuration.timeoutIntervalForResource = 10
    alamoFireManager = Alamofire.SessionManager(configuration: configuration)

let fileURL = Bundle.main.url(forResource: "MyImage", withExtension: "jpg")

alamoFireManager.upload(fileURL,
                            to: url,
                            method: .post,
                            headers: headers)
        .validate()
        .responseJSON {}

I tried to pass Data() to upload request also I tried pass reference to zip dit URL to upload request, also I tried InputStream(url: fileURL!)! but without success...

What am I doing wrong? how to send zip data to the server?

if there is some question feel free to ask!

Sirop4ik
  • 4,543
  • 2
  • 54
  • 121
  • why not the filepath of the zip file with extension `zip`? – muescha Feb 01 '17 at 12:58
  • you use the same url? did your endpoint can handle zip files? – muescha Feb 01 '17 at 12:59
  • maybe also your timeout with 10seconds is very short – muescha Feb 01 '17 at 13:00
  • add this to see how much is uploaded: ` .uploadProgress { progress in // Called on main dispatch queue by default print("Upload progress: \(progress.fractionCompleted)") }` – muescha Feb 01 '17 at 13:02
  • 1
    what you exactly mean with `zip dir`? its a zip file – muescha Feb 01 '17 at 13:07
  • try this example with your zip file https://github.com/Alamofire/Alamofire#uploading-a-file – muescha Feb 01 '17 at 13:08
  • @muescha thanks ! I have already solve... It was because of server side, but quite strange why I was getting timeout exception instead of some error... anyway now it is ok – Sirop4ik Feb 01 '17 at 13:19
  • @muescha maybe you know how to help me with this question? http://stackoverflow.com/questions/41982122/how-to-execute-alamofire-background-upload-request – Sirop4ik Feb 01 '17 at 14:33
  • please update your question or delete or add an answer with your solution - this helps others to waste no time with your not as solved marked question. – muescha Feb 01 '17 at 14:51
  • @muescha if you would like, you can add answer I will accept it. Because you give me idea to check server side. If not I will add answer for myself – Sirop4ik Feb 01 '17 at 15:27
  • pls you do, because you find the answer. and yes it is possible you can answer your own question and accept it :) – muescha Feb 01 '17 at 16:43

1 Answers1

0

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.

Steve Scheffler
  • 3,706
  • 2
  • 21
  • 22
Sirop4ik
  • 4,543
  • 2
  • 54
  • 121