在快速选择/捕获后获取文件路径给出错误域=nscocoaerrordomain code=260
Posted
技术标签:
【中文标题】在快速选择/捕获后获取文件路径给出错误域=nscocoaerrordomain code=260【英文标题】:Getting path of file after picking/capturing in swift gives error domain=nscocoaerrordomain code=260 【发布时间】:2020-05-16 19:02:49 【问题描述】:我在从图库中选择文件/从相机捕获文件后尝试上传文件,但文件路径出现错误 domain=nscocoaerrordomain code=260,我不确定为什么会出现此错误,是否与真的有关未找到的文件路径,如果是这样,那么我们在
中得到的路径是什么//来自画廊
self.selectedImageURL = "\(info[UIImagePickerController.InfoKey.imageURL]!)"
//来自相机
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)")
获取 URL 的示例: file:///private/var/mobile/Containers/Data/Application/F0A1163F-B8E4-43CA-BE70-3FF0218FB328/tmp/61E3FCBA-1A45-4A53-8877-A9392DACEFD4.jpeg
上传图片代码:
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)
我希望如果有人可以帮助我了解这里的问题,我搜索了有人说这是缓存路径而不是本地路径,但所有代码都只是给出了这种获取路径的方式!
感谢您的提前。
【问题讨论】:
【参考方案1】:原因: NSFileReadNoSuchFileError = 260, // Read error (no such file)
解决方案:确保文件存在,在终端上运行:
[ -e /path-to-file/file-name.file-extension ] && echo "File exists" || echo "File does not exist"
看看它是否返回File exists
【讨论】:
好的,它给出的文件不存在,但给定的 URL 是从移动端读取图库中的真实图像,所以如果从图库中选择的图像的给定路径是错误的,那么我们如何才能得到正确的拾取图像的路径?【参考方案2】:我遇到了同样的问题,我在 this SO post 找到了解决方案。
你需要改变这个:
let photoURL = URL.init(fileURLWithPath: localPath)
到这里:
let photoUrl = NSURL(fileURLWithPath: localPath!).path!
您还需要在 PUT 请求中进行更改:
let fileContent = String(data: fileData, encoding: .utf8)
希望对你有帮助!
【讨论】:
以上是关于在快速选择/捕获后获取文件路径给出错误域=nscocoaerrordomain code=260的主要内容,如果未能解决你的问题,请参考以下文章
获取未捕获的类型错误:path.split 不是反应中的函数
获取文档选择器的路径后,iOS 上的 React-Native-pdf 加载失败