Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

swift - How to upload zip data with alamofire?

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...