uploadTaskWithRequest:fromFile: fails randomly with NSInvalidArgumentException Reason:Cannot read file at

I am trying to upload a file,



if fileManager.fileExists(atPath: nsFileUrl.path) {

nsUrlSessionUploadTask = nsSession.uploadTask(with: request as URLRequest, fromFile: nsFileUrl)

}


I have added a check if file exists and then launch upload task but the app randomly crashes with


NSInvalidArgumentException Reason:Cannot read file at path

I have two questions


1. What could the reason be? Can it happen if the file got deleted from a different thread, after if check passes

2. Is there a way to handle this exception?

Replies

1. What could the reason be?

Last I checked the specific cause of that exception is that the file is not readable within the user’s sandbox. This typically means that one of the following applies:

  • The path doesn’t exist

  • The file system object it references isn’t readable

  • The file system object it references can’t be read due to sandbox restrictions

Can it happen if the file got deleted from a different thread, after if check passes

That’s certainly one possibility.

2. Is there a way to handle this exception?

No. The general Cocoa philosophy is that (except in very specific circumstances) the frameworks only throw language exceptions when they encounter a fatal error. The frameworks don’t attempt to clean up after throwing such an exception, so you can’t recover.

You will need to find a way to avoid this exception.

… the app randomly crashes with …

Can your reproduce this yourself? If so, you should set a exception breakpoint and then, when it throws the exception, inspect the state of the app to see what’s gone wrong.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Hi Eskimo,

I have one issue while uploading the file in background mode. Issue is some time when I try to upload file via uploadTaskWithRequest:request fromFile:[url filePathURL]] function I get error Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Cannot read file at file:///private/var/mobile/Containers/Shared/AppGroup/…. And some time it works. This is confusing as I used this before calling a function



if([[[NSFileManager alloc]init] fileExistsAtPath:url.path]){

NSLog(@"yes file is there and is it accessable ----%d",[[NSFileManagerdefaultManager]isReadableFileAtPath:url.path]);

NSData * theData = [NSData dataWithContentsOfFile: url.path

options: NSMappedRea error: nil];

NSLog(@"file size is ----%ld",theData.length);

}


And it says file exist on the given path and it always reads the file and prints the file length. I am not modifying the file anywhere so file should be there all the time.

Can you tell me what is wrong here?

The snippet of path included in the error message indicates that the file is store in an App Group container. Is that always the case when things fail?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

yes its always same. I am developing fileprovider extension. at the moment I resolved this error by using


uploadTaskWithStreamedRequest and it works always, it is strange that uploadTaskWithRequest:request fromFIle fails sometime.

at the moment I resolved this error by using

-uploadTaskWithStreamedRequest:
and it works always

Hmmm. The fact that

-uploadTaskWithStreamedRequest:
works implies that you’re not using a background session. Is that correct?

I was previously assuming you were using a background session, and background sessions introduce a whole world of extra complexity here. If you’re using a standard session then I need to reconsider the range of potential problems.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Hi eskimo

My file upload fails with the same exception-


*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Cannot read file at file:///var/mobile/Containers/Data/Application/xxxxxxxx-72A8-***-XXXX-6CCE12FBEE44/Library/Caches/downloads/87605777-1934-566f-92ac-379c76300cd8/C8D67486-B79F-44C7-9295-768F0D4D0323/appium%20!@%23$%25%5E&_-+=%7B%7Dl%5B%5D%20;'.,_%20%20;%20logs%20-%20Copy%20(1).ppt'


But my file upload fails when file name( as above ) contains ";" (semicolon)


Please note I'm using "uploadTask" to upload a file.


Thanks in advance.