I'm trying to upload file after picking it from gallery/capturing from camera but it gives error domain=nscocoaerrordomain code=260 for the file path, I'm not sure why I got this error and if it's related to really unfound file path, and if it is so what is the path we got then in
//from gallery
self.selectedImageURL = "\(info[UIImagePickerController.InfoKey.imageURL]!)"
//from Camera
if let originalImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage{
self.profileImage.image = originalImage
let imgName = UUID().uuidString
let documentDirectory = NSTemporaryDirectory()
let localPath = documentDirectory.appending(imgName)
let data = originalImage.jpegData(compressionQuality: 0.3)! as NSData
data.write(toFile: localPath, atomically: true)
let photoURL = URL.init(fileURLWithPath: localPath)
selectedImageURL = "\(photoURL)"
print("url is \(selectedImageURL)")
}
Example for the getting URL: file:///private/var/mobile/Containers/Data/Application/F0A1163F-B8E4-43CA-BE70-3FF0218FB328/tmp/61E3FCBA-1A45-4A53-8877-A9392DACEFD4.jpeg
code of uploading image:
let parameters = [
[
"key": "avatar",
"src": urlOfImage,
"type": "file"
],
[
"key": "user_id",
"value": UserDefaults.standard.getUserID(),
"type": "text"
]] as [[String : Any]]
let boundary = "Boundary-\(UUID().uuidString)"
var body = ""
var error: Error? = nil
for param in parameters {
if param["disabled"] == nil {
let paramName = param["key"]!
body += "--\(boundary)\r\n"
body += "Content-Disposition:form-data; name=\"\(paramName)\""
let paramType = param["type"] as! String
if paramType == "text" {
let paramValue = param["value"] as! String
body += "\r\n\r\n\(paramValue)\r\n"
} else {
let paramSrc = param["src"] as! String
do{
let fileData = try NSData(contentsOfFile:paramSrc, options:[]) as Data
let fileContent = String(data: fileData, encoding: .utf8)!
body += "; filename=\"\(paramSrc)\"\r\n"
+ "Content-Type: \"content-type header\"\r\n\r\n\(fileContent)\r\n"
}catch
{
//it gives error here for this file path doesn't exist
print(error)
}
}
}
}
body += "--\(boundary)--\r\n";
let postData = body.data(using: .utf8)
var request = URLRequest(url: URL(string: my_url)!,timeoutInterval: Double.infinity)
request.addValue(UserDefaults.standard.getUserToken(), forHTTPHeaderField: "x-access-token")
request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.addValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST"
request.httpBody = postData
doParsingUpload(request: request)
I hope if anyone can help me to know the problem here, I searched somebody said this is cache path not local path but all codes just gives this way to get path!
Thanks for advance.