I am trying to download this picture in my code using Alamofire 4.0.0 with Xcode 8.0 and Swift 3.0.
Here is my request:
func download(_ path: String, _ completionHandler: @escaping (Any?) -> ()) {
let stringURL = "https://slove.tulleb.com/uploads/6/6/0/2/66027561/2791411.jpg-1447979839.png"
print("Requesting \(stringURL)...")
_ = Alamofire.download(stringURL)
.responseData { response in
print(response)
if let data = response.result.value {
completionHandler(UIImage(data: data))
} else {
completionHandler(nil)
}
}
}
I have the following error:
FAILURE:
responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.inputFileReadFailed(file:///private/var/mobile/Containers/Data/Application/50400F41-47FD-4276-8903-F48D942D064A/tmp/CFNetworkDownload_D1Aqkh.tmp))
This problem occures whatever image link I am using.
Any help would be very appreciated, thanks :)
How can send http post request using Almofire with http header .and authentication?
1.parameters
let parameters: Parameters = ["username ": "Henry","password":"xxxx","key"="ewq2356"]
2.http header
let headers = [ "Authorization": "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==", "Content-Type": "application/x-www-form-urlencoded" ]
3.Authentication with URLCredential
let user = "user1" let password = "password1"
4.URL
http://myapi.com/rest/index.php
So how could I use it using Almofire http post method ?
@rakeshbiswalmobileprogrmming There is quite a bit in our README about adding authorization headers or using our .authenticate() function. Please read it and I think your questions will be answered.
@cnoon Perhaps this is related to #1553?
@jshier @cnoon just to remind you that I still have this download issue...
It doesn't seem related to @rakeshbiswalmobileprogrmming's problem :(
For me, some is success, some is always fail.
[Alamofire] download : https://0.s3.envato.com/files/47374385/akedodee_avatar.jpg
[Alamofire] download : https://preview-tf.s3.envato.com/files/200537021/Thumbnail.png
[Alamofire] failure : https://preview-tf.s3.envato.com/files/200537021/Thumbnail.png
[Alamofire] failure : https://0.s3.envato.com/files/47374385/akedodee_avatar.jpg
[Alamofire] download : https://preview-tf.s3.envato.com/files/126954540/Thumbnail.png
[Alamofire] download : https://preview-tf.s3.envato.com/files/198807153/Thumbnail.png
[Alamofire] download : https://preview-tf.s3.envato.com/files/128581452/Thumbnail.png
[Alamofire] download : https://preview-tf.s3.envato.com/files/148293837/Thumbnail.png
[Alamofire] download : https://preview-tf.s3.envato.com/files/130631924/Thumbnail.png
[Alamofire] download : https://preview-tf.s3.envato.com/files/128056772/Thumbnail.png
[Alamofire] download : https://preview-tf.s3.envato.com/files/68729806/Thumbnail.png
[Alamofire] download : https://preview-tf.s3.envato.com/files/134305582/Thumbnail.png
[Alamofire] download : https://preview-tf.s3.envato.com/files/135205962/Thumbnail.png
[Alamofire] success : https://preview-tf.s3.envato.com/files/126954540/Thumbnail.png
[Alamofire] success : https://preview-tf.s3.envato.com/files/128581452/Thumbnail.png
[Alamofire] success : https://preview-tf.s3.envato.com/files/130631924/Thumbnail.png
[Alamofire] success : https://preview-tf.s3.envato.com/files/68729806/Thumbnail.png
[Alamofire] failure : https://preview-tf.s3.envato.com/files/198807153/Thumbnail.png
[Alamofire] failure : https://preview-tf.s3.envato.com/files/134305582/Thumbnail.png
[Alamofire] failure : https://preview-tf.s3.envato.com/files/148293837/Thumbnail.png
[Alamofire] download : https://preview-tf.s3.envato.com/files/126954540/Thumbnail.png
[Alamofire] download : https://preview-tf.s3.envato.com/files/198807153/Thumbnail.png
[Alamofire] download : https://preview-tf.s3.envato.com/files/128581452/Thumbnail.png
[Alamofire] download : https://preview-tf.s3.envato.com/files/148293837/Thumbnail.png
[Alamofire] download : https://preview-tf.s3.envato.com/files/130631924/Thumbnail.png
[Alamofire] download : https://preview-tf.s3.envato.com/files/128056772/Thumbnail.png
[Alamofire] download : https://preview-tf.s3.envato.com/files/68729806/Thumbnail.png
[Alamofire] download : https://preview-tf.s3.envato.com/files/134305582/Thumbnail.png
[Alamofire] download : https://preview-tf.s3.envato.com/files/135205962/Thumbnail.png
[Alamofire] failure : https://preview-tf.s3.envato.com/files/135205962/Thumbnail.png
[Alamofire] failure : https://preview-tf.s3.envato.com/files/126954540/Thumbnail.png
[Alamofire] failure : https://preview-tf.s3.envato.com/files/198807153/Thumbnail.png
[Alamofire] failure : https://preview-tf.s3.envato.com/files/128056772/Thumbnail.png
[Alamofire] failure : https://preview-tf.s3.envato.com/files/128581452/Thumbnail.png
[Alamofire] failure : https://preview-tf.s3.envato.com/files/148293837/Thumbnail.png
[Alamofire] failure : https://preview-tf.s3.envato.com/files/130631924/Thumbnail.png
[Alamofire] failure : https://preview-tf.s3.envato.com/files/128056772/Thumbnail.png
[Alamofire] failure : https://preview-tf.s3.envato.com/files/68729806/Thumbnail.png
[Alamofire] failure : https://preview-tf.s3.envato.com/files/134305582/Thumbnail.png
[Alamofire] failure : https://preview-tf.s3.envato.com/files/135205962/Thumbnail.png
Test via simulator, BTW this never happen before, just after migrate both Swift and Alamofire everything (you know what I mean) gone wrong.
@katopz is it a ResponseSerializationFailureReason issue too?
Plus, the picture which are working/not working are always the same? Or does it change when you try to download them again?
@Tulleb Here's 7 hrs later results
// First page
[Alamofire] download : https://0.s3.envato.com/files/47374385/akedodee_avatar.jpg
[Alamofire] download : https://preview-tf.s3.envato.com/files/202311201/Thumbnail.png
[Alamofire] failure : https://preview-tf.s3.envato.com/files/202311201/Thumbnail.png
[Alamofire] error : responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.inputFileReadFailed(file:///Users/katopz/Library/Developer/CoreSimulator/Devices/E22F2B6B-FF24-45E4-8FA6-6028FF89AA8A/data/Containers/Data/Application/EF57E55A-FDEE-4683-9AA1-C46BECD69DD5/tmp/CFNetworkDownload_3HGEhQ.tmp))
[Alamofire] failure : https://0.s3.envato.com/files/47374385/akedodee_avatar.jpg
[Alamofire] error : responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.inputFileReadFailed(file:///Users/katopz/Library/Developer/CoreSimulator/Devices/E22F2B6B-FF24-45E4-8FA6-6028FF89AA8A/data/Containers/Data/Application/EF57E55A-FDEE-4683-9AA1-C46BECD69DD5/tmp/CFNetworkDownload_kOpQFs.tmp))
// Wait for a while Click and go to second page
[Alamofire] download : https://preview-tf.s3.envato.com/files/126954540/Thumbnail.png
[Alamofire] download : https://preview-tf.s3.envato.com/files/198807153/Thumbnail.png
[Alamofire] download : https://preview-tf.s3.envato.com/files/128581452/Thumbnail.png
[Alamofire] download : https://preview-tf.s3.envato.com/files/148293837/Thumbnail.png
[Alamofire] download : https://preview-tf.s3.envato.com/files/130631924/Thumbnail.png
[Alamofire] download : https://preview-tf.s3.envato.com/files/128056772/Thumbnail.png
[Alamofire] download : https://preview-tf.s3.envato.com/files/68729806/Thumbnail.png
[Alamofire] download : https://preview-tf.s3.envato.com/files/134305582/Thumbnail.png
[Alamofire] download : https://preview-tf.s3.envato.com/files/135205962/Thumbnail.png
[Alamofire] success : https://preview-tf.s3.envato.com/files/198807153/Thumbnail.png
[Alamofire] failure : https://preview-tf.s3.envato.com/files/130631924/Thumbnail.png
[Alamofire] error : responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.inputFileReadFailed(file:///Users/katopz/Library/Developer/CoreSimulator/Devices/E22F2B6B-FF24-45E4-8FA6-6028FF89AA8A/data/Containers/Data/Application/EF57E55A-FDEE-4683-9AA1-C46BECD69DD5/tmp/CFNetworkDownload_csuAV9.tmp))
[Alamofire] failure : https://preview-tf.s3.envato.com/files/148293837/Thumbnail.png
[Alamofire] error : responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.inputFileReadFailed(file:///Users/katopz/Library/Developer/CoreSimulator/Devices/E22F2B6B-FF24-45E4-8FA6-6028FF89AA8A/data/Containers/Data/Application/EF57E55A-FDEE-4683-9AA1-C46BECD69DD5/tmp/CFNetworkDownload_5GdHjl.tmp))
[Alamofire] download : https://preview-tf.s3.envato.com/files/126954540/Thumbnail.png
[Alamofire] download : https://preview-tf.s3.envato.com/files/198807153/Thumbnail.png
[Alamofire] download : https://preview-tf.s3.envato.com/files/128581452/Thumbnail.png
[Alamofire] download : https://preview-tf.s3.envato.com/files/148293837/Thumbnail.png
[Alamofire] download : https://preview-tf.s3.envato.com/files/130631924/Thumbnail.png
[Alamofire] download : https://preview-tf.s3.envato.com/files/128056772/Thumbnail.png
[Alamofire] download : https://preview-tf.s3.envato.com/files/68729806/Thumbnail.png
[Alamofire] download : https://preview-tf.s3.envato.com/files/134305582/Thumbnail.png
[Alamofire] download : https://preview-tf.s3.envato.com/files/135205962/Thumbnail.png
[Alamofire] failure : https://preview-tf.s3.envato.com/files/128056772/Thumbnail.png
[Alamofire] error : responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.inputFileReadFailed(file:///Users/katopz/Library/Developer/CoreSimulator/Devices/E22F2B6B-FF24-45E4-8FA6-6028FF89AA8A/data/Containers/Data/Application/EF57E55A-FDEE-4683-9AA1-C46BECD69DD5/tmp/CFNetworkDownload_AT2GKj.tmp))
[Alamofire] failure : https://preview-tf.s3.envato.com/files/68729806/Thumbnail.png
[Alamofire] error : responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.inputFileReadFailed(file:///Users/katopz/Library/Developer/CoreSimulator/Devices/E22F2B6B-FF24-45E4-8FA6-6028FF89AA8A/data/Containers/Data/Application/EF57E55A-FDEE-4683-9AA1-C46BECD69DD5/tmp/CFNetworkDownload_5RRaCL.tmp))
[Alamofire] failure : https://preview-tf.s3.envato.com/files/135205962/Thumbnail.png
[Alamofire] error : responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.inputFileReadFailed(file:///Users/katopz/Library/Developer/CoreSimulator/Devices/E22F2B6B-FF24-45E4-8FA6-6028FF89AA8A/data/Containers/Data/Application/EF57E55A-FDEE-4683-9AA1-C46BECD69DD5/tmp/CFNetworkDownload_xUtpSE.tmp))
[Alamofire] failure : https://preview-tf.s3.envato.com/files/126954540/Thumbnail.png
[Alamofire] error : responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.inputFileReadFailed(file:///Users/katopz/Library/Developer/CoreSimulator/Devices/E22F2B6B-FF24-45E4-8FA6-6028FF89AA8A/data/Containers/Data/Application/EF57E55A-FDEE-4683-9AA1-C46BECD69DD5/tmp/CFNetworkDownload_8nu8sb.tmp))
[Alamofire] failure : https://preview-tf.s3.envato.com/files/198807153/Thumbnail.png
[Alamofire] error : responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.inputFileReadFailed(file:///Users/katopz/Library/Developer/CoreSimulator/Devices/E22F2B6B-FF24-45E4-8FA6-6028FF89AA8A/data/Containers/Data/Application/EF57E55A-FDEE-4683-9AA1-C46BECD69DD5/tmp/CFNetworkDownload_Mx5IQS.tmp))
[Alamofire] failure : https://preview-tf.s3.envato.com/files/128581452/Thumbnail.png
[Alamofire] error : responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.inputFileReadFailed(file:///Users/katopz/Library/Developer/CoreSimulator/Devices/E22F2B6B-FF24-45E4-8FA6-6028FF89AA8A/data/Containers/Data/Application/EF57E55A-FDEE-4683-9AA1-C46BECD69DD5/tmp/CFNetworkDownload_oV4xjD.tmp))
[Alamofire] failure : https://preview-tf.s3.envato.com/files/148293837/Thumbnail.png
[Alamofire] error : responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.inputFileReadFailed(file:///Users/katopz/Library/Developer/CoreSimulator/Devices/E22F2B6B-FF24-45E4-8FA6-6028FF89AA8A/data/Containers/Data/Application/EF57E55A-FDEE-4683-9AA1-C46BECD69DD5/tmp/CFNetworkDownload_PGQyhr.tmp))
[Alamofire] failure : https://preview-tf.s3.envato.com/files/126954540/Thumbnail.png
[Alamofire] error : responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.inputFileReadFailed(file:///Users/katopz/Library/Developer/CoreSimulator/Devices/E22F2B6B-FF24-45E4-8FA6-6028FF89AA8A/data/Containers/Data/Application/EF57E55A-FDEE-4683-9AA1-C46BECD69DD5/tmp/CFNetworkDownload_hm7i4d.tmp))
[Alamofire] failure : https://preview-tf.s3.envato.com/files/130631924/Thumbnail.png
[Alamofire] error : responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.inputFileReadFailed(file:///Users/katopz/Library/Developer/CoreSimulator/Devices/E22F2B6B-FF24-45E4-8FA6-6028FF89AA8A/data/Containers/Data/Application/EF57E55A-FDEE-4683-9AA1-C46BECD69DD5/tmp/CFNetworkDownload_VpiYPk.tmp))
[Alamofire] failure : https://preview-tf.s3.envato.com/files/134305582/Thumbnail.png
[Alamofire] error : responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.inputFileReadFailed(file:///Users/katopz/Library/Developer/CoreSimulator/Devices/E22F2B6B-FF24-45E4-8FA6-6028FF89AA8A/data/Containers/Data/Application/EF57E55A-FDEE-4683-9AA1-C46BECD69DD5/tmp/CFNetworkDownload_sWPj6h.tmp))
[Alamofire] failure : https://preview-tf.s3.envato.com/files/68729806/Thumbnail.png
[Alamofire] error : responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.inputFileReadFailed(file:///Users/katopz/Library/Developer/CoreSimulator/Devices/E22F2B6B-FF24-45E4-8FA6-6028FF89AA8A/data/Containers/Data/Application/EF57E55A-FDEE-4683-9AA1-C46BECD69DD5/tmp/CFNetworkDownload_Thkfnl.tmp))
[Alamofire] failure : https://preview-tf.s3.envato.com/files/128056772/Thumbnail.png
[Alamofire] error : responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.inputFileReadFailed(file:///Users/katopz/Library/Developer/CoreSimulator/Devices/E22F2B6B-FF24-45E4-8FA6-6028FF89AA8A/data/Containers/Data/Application/EF57E55A-FDEE-4683-9AA1-C46BECD69DD5/tmp/CFNetworkDownload_OxyIf5.tmp))
[Alamofire] failure : https://preview-tf.s3.envato.com/files/134305582/Thumbnail.png
[Alamofire] error : responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.inputFileReadFailed(file:///Users/katopz/Library/Developer/CoreSimulator/Devices/E22F2B6B-FF24-45E4-8FA6-6028FF89AA8A/data/Containers/Data/Application/EF57E55A-FDEE-4683-9AA1-C46BECD69DD5/tmp/CFNetworkDownload_hgkxpi.tmp))
[Alamofire] failure : https://preview-tf.s3.envato.com/files/128581452/Thumbnail.png
[Alamofire] error : responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.inputFileReadFailed(file:///Users/katopz/Library/Developer/CoreSimulator/Devices/E22F2B6B-FF24-45E4-8FA6-6028FF89AA8A/data/Containers/Data/Application/EF57E55A-FDEE-4683-9AA1-C46BECD69DD5/tmp/CFNetworkDownload_dQuDpf.tmp))
[Alamofire] failure : https://preview-tf.s3.envato.com/files/135205962/Thumbnail.png
[Alamofire] error : responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.inputFileReadFailed(file:///Users/katopz/Library/Developer/CoreSimulator/Devices/E22F2B6B-FF24-45E4-8FA6-6028FF89AA8A/data/Containers/Data/Application/EF57E55A-FDEE-4683-9AA1-C46BECD69DD5/tmp/CFNetworkDownload_Cl2stR.tmp))
Seem like when we load same image e.g. https://preview-tf.s3.envato.com/files/198807153/Thumbnail.png can be both success and failed.
But anyhow 2 image at first page always failed for some reason.
Did it work on your machine?
Thanks
@Tulleb FYI In my case simple load with URLSession is working just fine tho 🍰
let _url = NSURL(string: url) as! URL
let dataTask = URLSession.shared.dataTask(with: _url) {
data, response, error in
if error == nil {
if let data = data,
let image = UIImage(data: data) {
fulfill(image)
}
} else {
reject(error as! NSError)
}
}
dataTask.resume()
Yes I presume I'll use good old URLSession if Alamofire support is too busy at the moment.
Thanks for your help @katopz!
Hi @Tulleb,
Apologies for not getting back to you sooner. The example @katopz is not the same type of request. That example demonstrates how to use a data task, not a download task. If you don't wish to download the file, you can instead do the following:
Alamofire.request(url).responseData { response in
guard let data = response.result.value else { return }
let image = UIImage(data: data)
print(image)
}
However, to answer you're original question, you're running into a sandbox permissions issue. We allow you to use the download APIs without specifying a destination closure for operating systems like macOS where you can access files outside of your own sandbox. However, on iOS, you cannot directly access the data of a file that is outside of your sandbox. That's why you are seeing the .inputFileReadFailed error.
There are a couple ways you can solve this issue.
You can download the data using the request API as shown above which downloads the image data into memory, not to disk.
You can move the file into your sandbox before accessing the data using a destination closure. Here's an example of how to do that:
let destination: DownloadRequest.DownloadFileDestination = { _, _ in
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
let documentsURL = URL(fileURLWithPath: documentsPath, isDirectory: true)
let fileURL = documentsURL.appendingPathComponent("image.png")
return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
}
Alamofire.download("https://httpbin.org/image/png", to: destination).responseData { response in
debugPrint(response)
if let data = response.result.value {
let image = UIImage(data: data)
print(image)
} else {
print("Data was invalid")
}
}
// Outputs:
// [Request]: https://httpbin.org/image/png
// [Response]: <NSHTTPURLResponse: 0x170032940> { URL: https://httpbin.org/image/png } { status code: 200, headers {
// "Access-Control-Allow-Origin" = "*";
// "Content-Length" = 8090;
// "Content-Type" = "image/png";
// Date = "Sat, 24 Sep 2016 21:34:25 GMT";
// Server = nginx;
// "access-control-allow-credentials" = true;
// } }
// [TemporaryURL]: /private/var/mobile/Containers/Data/Application/25612024-9A05-4ED5-AF3B-A98E22DEAD7A/tmp/CFNetworkDownload_fD9sXf.tmp
// [DestinationURL]: /var/mobile/Containers/Data/Application/25612024-9A05-4ED5-AF3B-A98E22DEAD7A/Documents/image.png
// [ResumeData]: 0 bytes
// [Result]: SUCCESS: 8090 bytes
// [Timeline]: Timeline: { "Request Start Time": 496445664.792, "Initial Response Time": 496445665.651, "Request Completed Time": 496445665.655, "Serialization Completed Time": 496445665.655, "Latency": 0.860 secs, "Request Duration": 0.863 secs, "Serialization Duration": 0.000 secs, "Total Duration": 0.864 secs }
// Optional(<UIImage: 0x17008bc20>, {100, 100})
You MUST use a destination closure if you need to download the file to disk. The temporary file can only be accessed inside the delegate callback which is handled internally in Alamofire. If you do not specify a destination closure on iOS, the temporaryURL will always point to where the temp file was previously stored, but has been cleaned up.
So in summary, if you don't need to download the data to disk, then you want Option 1. If you do want to save the file on disk, then you want Option 2.
Cheers. 🍻
Thanks @cnoon
We follow README which apparently suggest us to use download image.
From my noob point of view, Please add this to README
Alamofire.request(url).responseData { response in
guard let data = response.result.value else { return }
let image = UIImage(data: data)
print(image)
}
and everyone should be happy. :)
Thanks guys, You're awesome.
@katopz says it all already.
Thanks a lot guys!!
@cnoon
So on macOS the location should not be removed after the delegate message returns?
but in my test result the file is also removed.
I think destination should be a required parameter.
@cnoon thank for your info about it.
I've found a strangeness: with something requests you can have a forbidden access (403) but data is valid:
Debug console during 403 forbidden with a valid response.result.value:
(lldb) po response.result.value
▿ Optional<Data>
▿ some : 0 bytes
- count : 0
▿ pointer : 0x0000bad0
- pointerValue : 47824
- bytes : 0 elements
Anyway, I've solved with this code:
Alamofire.download(url, to :destination)
.downloadProgress { progress in
print(progress)
}
.responseData { response in
let statusCode = response.response?.statusCode
if statusCode == 403 { // here response.result.value is valid
print("forbidden")
}
if let data = response.result.value {
let image = UIImage(data: data) // 403 status code come here if I don't stop before..
print(image)
} else {
print("Data was invalid")
}
}
}
There is another way to write this code?
Most helpful comment
Hi @Tulleb,
Apologies for not getting back to you sooner. The example @katopz is not the same type of request. That example demonstrates how to use a data task, not a download task. If you don't wish to download the file, you can instead do the following:
However, to answer you're original question, you're running into a sandbox permissions issue. We allow you to use the
downloadAPIs without specifying adestinationclosure for operating systems likemacOSwhere you can access files outside of your own sandbox. However, on iOS, you cannot directly access the data of a file that is outside of your sandbox. That's why you are seeing the.inputFileReadFailederror.There are a couple ways you can solve this issue.
Option 1
You can download the data using the
requestAPI as shown above which downloads the image data into memory, not to disk.Option 2
You can move the file into your sandbox before accessing the data using a
destinationclosure. Here's an example of how to do that:You MUST use a destination closure if you need to download the file to disk. The temporary file can only be accessed inside the delegate callback which is handled internally in Alamofire. If you do not specify a destination closure on iOS, the
temporaryURLwill always point to where the temp file was previously stored, but has been cleaned up.Summary
So in summary, if you don't need to download the data to disk, then you want Option 1. If you do want to save the file on disk, then you want Option 2.
Cheers. 🍻