Different action by prefix of URL

后端 未结 1 1968
孤城傲影
孤城傲影 2020-12-20 08:59

Here is my situation: i\'m calling file locally on my ios application ( Running in Swift).

If the file is a jpg, one action happen, if the file is a mp4, another act

相关标签:
1条回答
  • 2020-12-20 09:41

    You can make a URL request for the url header using the httpMethod HEAD to check your url mime type without the need to download the data first:

    let link = "https://www.dropbox.com/s/sk46eyglvijlrec/horse.jpg?dl=1"
    let url = URL(string: link)!
    
    var request = URLRequest(url: url)
    request.httpMethod = "HEAD"
    URLSession.shared.dataTask(with: request) { _ , response , _ in
        guard let response = response, (response as? HTTPURLResponse)?.statusCode == 200 else { return }
        DispatchQueue.main.async() {
            print("mimeType", response.mimeType ?? "nil")            // image/jpeg
            print("suggestedFilename:", response.suggestedFilename ?? "no suggestedFilename")   // horse.jpg
            print("expectedContentLength:", response.expectedContentLength ?? "nil")   // 352614
            print("textEncodingName:", response.textEncodingName ?? "nil")
            print("url:", response.url ?? "nil")                 // "https://dl.dropboxusercontent.com/content_link/RNrhGtvroTLU1Gww7eQo1N1ePRiix68zsqZJ1xWPjKm3pmOUNQwNVntbPuFG4jZ8/file?dl=1"
        }
    }.resume()
    
    0 讨论(0)
提交回复
热议问题