我正在尝试使用Alamofire在ssme时上传图像文件(.jpg)和pdf文件(.pdf)。我不断收到以下错误消息,该错误消息表示我尝试上传的文件不存在,实际上确实存在。
multipartEncodingFailed(Alamofire.AFError.MultipartEncodingFailureReason.bodyPartFileNotReachableWithError(atURL: file:///var/mobile/Containers/Data/Application/7FF74F06-CD6E-47D6-850E-45E768C00D97/Documents/tempImage_wb.jpg, error: Error Domain=NSCocoaErrorDomain Code=260 "The file “tempImage_wb.jpg” couldn’t be opened because there is no such file." UserInfo={NSURL=file:///var/mobile/Containers/Data/Application/7FF74F06-CD6E-47D6-850E-45E768C00D97/Documents/tempImage_wb.jpg, NSFilePath=/var/mobile/Containers/Data/Application/7FF74F06-CD6E-47D6-850E-45E768C00D97/Documents/tempImage_wb.jpg, NSUnderlyingError=0x1c4841020 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}))
我用于上传文件的代码:
// GET URL FOR IMAGE AND PDF FILES
guard
let imageURLString = UserDefaults.standard.string(forKey: "URL_IMAGE"),
let pdfURLString = UserDefaults.standard.string(forKey: "URL_PDF") else{return}
guard
let imgURL = URL.init(string: imageURLString),
let pdfURL = URL.init(string: pdfURLString) else{return}
var arrayURLToUpload: [URL] = []
arrayURLToUpload.append(imgURL)
arrayURLToUpload.append(pdfURL)
let sendParamters = ["user_id": "1", "hashTagArray": jsonArrayHashTags]
Alamofire.upload(
multipartFormData: { multipartFormData in
for(key, value) in sendParamters{
multipartFormData.append((value.data(using: .utf8)!), withName: key)
}
for fileURL in arrayURLToUpload{
print("fileURL: \(fileURL)")
multipartFormData.append(fileURL, withName: "file[]")
}
},
to: UPLOAD_URL,
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON { response in
debugPrint(response)
}
/**TRACK PROGRESS OF UPLOAD**/
upload.uploadProgress { progress in
print(progress.fractionCompleted)
}
/***/
case .failure(let encodingError):
print(encodingError)
}
}
)
控制台:
fileURL: file:///var/mobile/Containers/Data/Application/7FF74F06-CD6E-47D6-850E-45E768C00D97/Documents/tempImage_wb.jpg
fileURL: file:///var/mobile/Containers/Data/Application/7FF74F06-CD6E-47D6-850E-45E768C00D97/Documents/tempImagePDF.pdf
我知道文件的存在是因为我检索了其中一个文件(.pdf)并显示了它,因此我能够查看已存储的pdf文件:
guard let urlString = UserDefaults.standard.string(forKey: "URL_PDF") else{return}
guard let pdfURL = URL.init(string: urlString) else{
print("no pdf URL")
return
}
print("pdfURL: \(pdfURL)")
guard let pdf = PDFDocument.init(url: pdfURL) else{
print("NO PDF DOCUMENT FOUND")
return
}
pdfPreview.document = pdf // CAN VIEW PDF FILE!!!
pdfPreview.autoScales = true
print("pdf document displayed!")
控制台:
pdfURL: file:///var/mobile/Containers/Data/Application/90ECD1AE-B9A5-46C9-AD30-C5D8D850A361/Documents/tempImagePDF.pdf
pdf document displayed!
我如何生成我的URL:
// Create a URL to save PDF
func createPdfURL() -> URL {
let fileName = "tempImagePDF.pdf"
let documentsDirectories = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let documentDirectory = documentsDirectories.first!
let pdfPageURL = documentDirectory.appendingPathComponent("\(fileName)")
return pdfPageURL
}
然后我将PDF文档保存如下:
// SAVE PDF TO LOCAL FILE SYSTEM
func savePdfToLocaFile(pdf: PDFDocument) -> Void {
// CREATE URL FOR PDF DOCUMENT
let pdfURL = createPdfURL()
print("PDF SAVED TO URL: \(pdfURL)")
self.pdfDocumentURL = pdfURL
pdf.write(to: pdfURL)
}
我还尝试了以下方法,通过使用文件名重构URL,然后再使用它来上传文件:
// CONSTRUCT URL FROM FILE NAME
let imgFileName = "tempImage_wb.jpg"
let pdfFileName = "tempImagePDF.pdf"
var dir: URL!
do {
dir = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
} catch{
print("error re-creating url")
}
let imgURL = dir.appendingPathComponent(imgFileName)
let pdfURL = dir.appendingPathComponent(pdfFileName)
实施@vadian的建议后,新的控制台阅读内容:
PDF SAVED TO URL: file:///var/mobile/Containers/Data/Application/35C9D3EE-2D0C-4028-BCF3-2FE4581A0686/Documents/tempImagePDF.pdf
fileURL: file:///var/mobile/Containers/Data/Application/35C9D3EE-2D0C-4028-BCF3-2FE4581A0686/Documents/tempImagePDF.pdf
答案 0 :(得分:3)
请注意不同的应用程序容器标识符7FF74F06-CD6E-47D6-850E-45E768C00D97
和90ECD1AE-B9A5-46C9-AD30-C5D8D850A36
。
容器位置会定期更改,因此请不要将指向应用程序容器的完整路径保存到UserDefaults
。
始终使用
let documentsFolderURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
然后在文件名后附加.appendingPathComponent
注意:try!
是安全的,因为 document 目录始终由框架创建。