Cannot open a file in a Web App

I am trying to open a PDF file in a Web App and I cannot get this code (below) to work. Testing has shown It always ends up at the MsgBox(“Cannot open”) because the “f.Exists” fails but “f” is NOT nil. This is in DEBUG on OSX. I am POSITIVE the file exists and it will open if I double click the PDF via Finder.

I have tried the created directory that I want to use in my user account home directory and I have placed the file in the working directory where the program is running.

Does the standalone web server used for debugging not have permission to open the file? The permission is R/W for my account and Read Only for others.

Any insight would be appreciated.

  Dim fname as string
  Dim fpath as string
  Dim MyFile as WebFile
  Dim f as FolderItem
  
  fname = ListBoxPDFlist.Cell(ListBoxPDFlist.ListIndex, 0)
  fpath = FileListPathArray(ListBoxPDFlist.ListIndex)
  
  msgbox("("+fpath+fname+")")
  
  f = GetFolderItem(fname)
  
  if f <> Nil and f.Exists then
    MyFile = WebFile.Open(f)
    MyFile.ForceDownload = false
    ShowURL(MyFile.URL)
  else
    MsgBox("Cannot open")
  end if
  

I notice your message box uses fpath + fname while you’re only getting fname for f. If fname is only the file name it probably won’t be in the debug folder.

f = GetFolderItem(fname)

What about the path part of this? Shouldn’t it be:

f = getFolderItem(fpath + fname)

EDIT: Wayne beat me to it. :stuck_out_tongue:

And just to note:

f will be NIL only when you have specified an impossible path.

If you specify a file that doesn’t exist in a directory that does exist, then f will be non-nil and f.exists will be false.

Don’t count on f not nil meaning that the file exists.

Also, your logic on f not being nil is incorrect. Your code above is testing for both f <> nil and f.exists. This in no way guarantees that f is NOT nil as you stipulated - you don’t know which condition results in the msgbox, it could be both!

That said, I agree that omission of the path component is the real problem.

Sorry … This is a bit misleading. My last iteration was to copy the file I wanted to open to the same directory as the working directory for the executable (debug directory) and remove the path from the code.

Jay I can see you are correct … but I copied the code from an example in the XOJO help and did not give it much thought. I did do some other testing (not shown in the sample code) to test separately for the NIL value of “f” and the “f.exists” and the value was NOT NIL when I was using the full path or only the current working directory without a path. So Tim’s comment would indicate that the path must be correct.

The “f.exists” always failed no matter where the file was located. I even did a copy/paste of the output from this message box, msgbox("("+fpath+fname+")"), to get the EXACT path used by the program and I am 100% sure the path and file name do point to the file.

Sooooo … Maybe a permission problem???

What do you get if you add
msgbox f.nativepath

?

More new “evidence” …

I just learned that the “.debug” directory is removed and added back when you compile your project. My file was getting deleted. Not sure how I missed that other than being tired last night when I started this debugging. I really don’t want the file there anyway but I was trying to eliminate any issue of a path to another directory.

Wayne … thanks for sending me down that path (no pun intended).

SO … I copied the file back AFTER the compile and it now exists.

BUT … It appears to open a Webfile it must be a SESSION Property. I was getting a 404 error from a ShowURL until I made the Webfile a Session property.

AND … I still need to track down why I cannot open it in a different directory (not the .debug). Maybe it will just work now.

Use a CopyFilesStep to copy your file when you build.

WebFiles don’t need to be session properties if you set their Session property to Nil (thus making the file available to ALL Sessions).

I quickly modified the Downloading example from Xojo by simply adding a folderItem and making ForceDownload = False.

Sub Action() dim f as FolderItem = specialfolder.desktop.child("shuttlelaunch.png") App.LogoFile = WebFile.Open(f) // MyFile is a property on the App object App.LogoFile.ForceDownload = False If App.LogoFile <> Nil Then ShowURL(App.LogoFile.URL) Else MsgBox("The logo file is not available.") End If End Sub

This works perfectly, with a png.

But when I try to open a PDF instead, it simply does nothing. Am on a Mac and the same occurs with Safari and Chrome, both opening perfectly the same file if I drop it over. Something else must be going on.

Continuing experiments with the PDF file, I think there is something definitely wrong with PDF files in Xojo web.

The very same code with ForceDownload = True perfectly downloads the PDF file.

So the file is indeed seen correctly by the app. RTF files do download file as well, and txt files are displayed by the browser if ForceDownload is not true.

Now, why does ShowURL appear as it is canceling viewing of the PDF file and not other types ?

I verified that the file uploaded on my server is displayed correctly by the browser when the URL to it is used.

It very much looks like a bug. Or is there some hidden setting somewhere that intently and specifically suppresses PDF display ? Greg ?

QUESTION FOR GREG

If I want the Webfile to be private to that session would I make it a session property?

ALSO

The thing that made it work to open a file in a different directory was to add PathTypeNative to the GetFolderItem.

f = GetFolderItem(fpath + fname, FolderItem.PathTypeNative)

Previously I just defaulted to PathTypeAbsolute. I have not done much with handling files in a Web App and I was just not paying attention. :-((

[quote=108087:@Michel Bujardet]Continuing experiments with the PDF file, I think there is something definitely wrong with PDF files in Xojo web.

The very same code with ForceDownload = True perfectly downloads the PDF file.

So the file is indeed seen correctly by the app. RTF files do download file as well, and txt files are displayed by the browser if ForceDownload is not true.

Now, why does ShowURL appear as it is canceling viewing of the PDF file and not other types ?

I verified that the file uploaded on my server is displayed correctly by the browser when the URL to it is used.

It very much looks like a bug. Or is there some hidden setting somewhere that intently and specifically suppresses PDF display ? Greg ?[/quote]
It’s not a bug as far as I can tell. Usually the issue is in how the browser displays PDFs. For instance, if you have acrobat reader installed, you get a very different experience as when you don’t and you are relying on the browser to handle it without a plugin.

[quote=108106:@Mark Strickland]QUESTION FOR GREG

If I want the Webfile to be private to that session would I make it a session property?
[/quote]
If you want a WebFile to be private to a particular session, either create it while interacting with that session, or set the WebFile.Session property to the session you want it connected to.

Chrome does not need any plugin, it displays PDF without a plugin. If I place the file on my server and enter the URL, it displays fine.

If I try to display it through Xojo, nothing happens. The browser does not even offer to download it. No error either.

If I set ForceDownload to True, then the browser finds the file and downloads it fine.

Seems for some reason Xojo does not show the URL at all when it comes to showing a PDF file on Mac.

[quote=108106:@Mark Strickland]The thing that made it work to open a file in a different directory was to add PathTypeNative to the GetFolderItem.

[/quote]

So you are now able to display your PDF ? Which browser ? With a plugin ?

Are you setting the WebFile.MimeType property? It should be “application/pdf” for PDF files.

That fixed the issue :slight_smile:

Thank you Greg !