当前位置: 首页 > 知识库问答 >
问题:

无法打开文件“xxx.mp4”,因为您没有查看它的权限

淳于健
2023-03-14

我使用Alamofire方法multipartFormData.append(URL, with Name:"file")将视频上传到服务器,但我收到一条错误消息:

MultipartEncoding失败(Alamofire. AFError. MultipartEncodingFailureReason.bodyPartFileNotReachablewith Error(file:///var/mobile/Media/DCIM/100APple/IMG_0939.mp4, Error Domain=NSCoaErrorDomain Code=257"无法打开文件"IMG_0939.mp4",因为您无权查看它。"UserInfo={NSURL=file:///var/mobile/Media/DCIM/100APple/IMG_4, NSFilePath=/var/mobile/Media/DCIM/100APple/IMG_0939.mp4, NSUnderlyingError=0x174450d10{Error Domain=NSPOSIXErrorDomain Code=1"不允许操作"}}))

我已经打开了隐私照片库的使用说明,但我发现了这个错误,我不知道。

我的代码:

alamofireManager?.upload(
    multipartFormData: { multipartFormData in
        for i in 0..<videoURLs.count {
            multipartFormData.append(videoURLs[i], withName: "file")
        }
    },
    to: url + urlString,
    headers: headers,
    encodingCompletion: { encodingResult in
        switch encodingResult {
        case .success(let upload, _, _):
            upload.uploadProgress(closure: { (progress) in


                //Print progress
                //print(progress)
            })

            upload.responseJSON { response in
                if let value = response.result.value as? [String: AnyObject]{
                    success(value)
                }
            }
        case .failure(let encodingError):
            failture(encodingError)
        }
    }
)

共有1个答案

宗意蕴
2023-03-14

您需要使用startAccessingSecurityScopedResource

if let url = file.url, url.startAccessingSecurityScopedResource() {
    multipartFormData.append(url, withName: "some-file.pdf")
    // After using the resource make sure you stop the access
    if url.startAccessingSecurityScopedResource() {
        url.stopAccessingSecurityScopedResource()
    }
}
 类似资料: