I have got the URL of a file using file importer. Now I want to store the file in a variable so that I can send it to the server.
.fileImporter(isPresented: $openfile, allowedContentTypes: [.audio,.pdf,.png, .text, .jpeg], allowsMultipleSelection: false) { (res) in
do{
let fileURL = try res.get()
//getting file name
self.fileName = fileURL.last?.lastPathComponent ?? "File Uplaoded"
print(fileURL)
}catch{
print("Error reading docs")
print(error.localizedDescription)
}
}
How can I save the file in a variable from the fileURL?
The stackoverflow answer shows you how to go about getting the file content. You have to adapt the answer to your code, something like:
var fileData: Data?
.fileImporter(isPresented: $openfile, allowedContentTypes: [.audio,.pdf,.png, .text, .jpeg], allowsMultipleSelection: false) { (res) in
do{
let fileURL = try res.get()
if let url = fileURL.first {
fileName = url.lastPathComponent
fileData = try Data(contentsOf: url)
print("\n fileName: \(fileName) \n url: \(url) \n fileData: \(fileData)")
}
}catch{
print("Error reading docs")
print(error.localizedDescription)
}
}
Then you can use fileData to send to the server.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.