0
votes

I am trying to save xlsx file from document folder to app folder. Here is code but it showing error:

Error Domain=NSCocoaErrorDomain Code=260 "The file “file1.xlsx” couldn’t be opened because there is no such file." UserInfo={NSFilePath=file:///private/var/mobile/Containers/Data/Application/8AC91C23-3662-44FF-90EF-20F6A34AF61F/Documents/file1.xlsx, NSUnderlyingError=0x16169a60 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}

let directoryContents = try NSFileManager.defaultManager().contentsOfDirectoryAtURL( documentsUrl, includingPropertiesForKeys: nil, options: [])
let xlsxFiles = directoryContents.filter{ $0.pathExtension == "xlsx" }
let xlsxFileNames = mp3Files.flatMap({$0.URLByDeletingPathExtension?.lastPathComponent})

var fileManager = NSFileManager.defaultManager()

do {
    try fileManager.copyItemAtPath(xlsxFiles[0].absoluteString, toPath: NSBundle.mainBundle().resourcePath! + xlsxFileNames[0] + ".xlsx")
}
catch let error 
{
    print(error)
}
1
On a device, the application bundle is readonly. Why do you think the file should be in the bundle?Phillip Mills
Run on simulator and check manually if file exists.Nikhil Manapure
@PhillipMills he must have put that file in bundle file creating project.Nikhil Manapure
@meet adding file to project doesn't ensure that it's added to bundle, you will need to add the file to target too.Nikhil Manapure
@PhillipMills github.com/renebigot/XlsxReaderWriter using this library which opens file from bundle only this is how this library finds path of excel file NSBundle.mainBundle().pathForResource("testWorkbook", ofType: "xlsx") any idea what should i do ?Meet Desai

1 Answers

2
votes

You have two problems. The first one is causing the error you posted in your question.

  1. Both parameters to copyItemAtPath need to be full file paths. Your first argument is just a file name. You need to append the file name to a full path to its location (similar to what you do for the second argument).
  2. The app's bundle is read-only. You can't copy a file to your app's bundle. So even once you fix the first problem, you will have a new problem. If the file is already inside your app's Documents folder, why do you try to copy it to the bundle?