Problem Xojo.IO.TextOutputStream and special characters in path

Hi,

I’m having a strange problems using Xojo.net.HTTPSocket.
I download html pages and images from websites. To cache the files, I save them into a subfolder in SpecialFolder.Preferences for PC or SpecialFolder.ApplicationData for Mac

I call my Xojo.Net.HTTPSocket with something like this :

Dim newPath As New Xojo.IO.FolderItem(PreferencesGetTemporaryFolder.NativePath.ToText) // PreferencesGetTemporaryFolder returns the classic path outputFile = newPath.Child( GenerateUUID.ToText ) // Create unique name for cache file ScriptRecherche.FileCache.Value( url ) = outputFile // Save the name in a dictionary key = url, value = FolderItem WRechercheInternet.XojoSocketListe.Send(methode.ToText, url.ToText, outputFile ) // Call the socket. methode = GET

On Mac there’s no problems. But on Windows … it depends ! Some users said that they got an error message. After verifying, that’s because the fil is not created. I know the the app has access to the folder, because I’m writing a lot of files in it (with classic HTTPScocket or just from local data)

The Xojo.Net.HTTPSocket fires the FileReceived event. I’ve added control code in the Error event but this event is not called. But there’s no file in the folder.

I’ve tried using the PageReceived method and write the file myself :

[code]Dim contentData As Text
contentData = Xojo.Core.TextEncoding.FromIANAName( ListeTitres.Encodage ).ConvertDataToText( content )

Dim outputFile as Xojo.IO.FolderItem
Dim newPath As New Xojo.IO.FolderItem(PreferencesGetTemporaryFolder.NativePath.ToText)
outputFile = newPath.Child( GenerateUUID.ToText )

Dim output As TextOutputStream
Try
Dim encoding As Xojo.Core.TextEncoding
encoding = Xojo.Core.TextEncoding.FromIANAName( ListeTitres.Encodage )
output = TextOutputStream.Create( outputFile, encoding )
output.Write Content
output.Close
Catch err As IOException
… (show error code)
End Try
[/code]
The err.code =0 and the err.Message is “The file could not be created” (I suppose the FileReceived had the same problem)

I’ve also try to use SpecialFolder.Temporary … with the same error !

I’m not sure this is because of some antivirus software. I don’t know how many of my PC users have this problems. Of course, I’ve no problem with all my test machine (VM W7, VM W10, VM W10 with Avast, Surface 3)

There’s no problem downloading and writing file with classic framework on the same location. Can it be a problem with the new Xojo framework ? I’m very confused…

Bonjour Pascal,

In the code you pasted it seems that you are mixing the classic and new xojo frameworks.

Dim output As TextOutputStream

Could you try with

Dim output As Xojo.IO.TextOutputStream Try Dim encoding As Xojo.Core.TextEncoding encoding = Xojo.Core.TextEncoding.FromIANAName( ListeTitres.Encodage ) output = TextOutputStream.Create( outputFile, encoding ) output.Write Content output.Close Catch err As Xojo.Core.IOException ... (show error code) End Try

I also found this thread that might be similar
https://forum.xojo.com/4715-specialfolder-preferences-returns-incorrect-value/0

The PageReceived starts with

Using Xojo.Core Using Xojo.IO

As I said, I’ve no problem on my test machines (on my Mac everything works like a charm), so to test new things, I need to create a new exe and send it to some testers. No very fast and efficient :frowning:

Did you try reproducing this issue with a Windows VM ?

I’ld like that, but since I don’t know what is the problem, I can’t reproduce it.

If you want to try, the app is available here (32-bit and 64-bit) : https://www.filmotech.info/download.php

I’m only on Mac, sorry I can’t help much more

[quote]SpecialFolder.Preferences for PC or SpecialFolder.ApplicationData for Mac

[/quote]
Out of curiosity, why are you not just using SpecialFolder.ApplicationData for both platforms. Don’t know that would make a difference, but why create complexity where it’s not needed?

Good point, probably because this part is old code, so it’s for historical reasons ;).
But on PC, SpecialFolder.ApplicationData and SpecialFolder.Preferences are the same : \Users\UserName\AppData\Roaming\

I would recommend you use the TPSF module for classic framework FolderItems and getting the correct location on both Mac and Windows with no effort on your part.

You need to be using a subfolder in ApplicationData, not the ApplicationData folder itself. The module handles all of this for you :slight_smile: (just use TPSF.AppSupport in place of SpecialFolder.ApplicationData)

https://github.com/devtimi/TPSF

I’ve made a test program for my users having problem. The test program try many things, but here is what does not work for them :

I create the temp folder with Classic FolderItem (I’ve removed debug messages and return code test from this code)

[code]Dim f as FolderItem
f = SpecialFolder.Preferences

preferenceFolder = f.Child(“Filmotech_prefs”)
if preferenceFolder<> nil and not preferenceFolder.Exists then
preferenceFolder.CreateAsFolder
end if

// Temporary folder (AppData)
tempFolder = preferenceFolder.Child(“temp”)

if tempFolder <> nil then
tempFolder.CreateAsFolder
end if
[/code]

Then, I’ve test a classic TextOutputStream

[code]Dim f as FolderItem
Dim t as TextOutputStream

f = tempFolder.Child( GenerateUUID )

If f <> Nil then
t = TextOutputStream.Append(f)
t.Write(EFResultat.Text)
t.Close
End If
[/code]

The file was created without error

Then, with Xojo.IO.TextOutputStream

[code]Dim outputFile as Xojo.IO.FolderItem
Dim newPath As New Xojo.IO.FolderItem(tempFolder.NativePath.ToText)
outputFile = newPath.Child( GenerateUUID.ToText )

Try
Dim output As Xojo.IO.TextOutputStream
output = Xojo.IO.TextOutputStream.Create(outputFile, Xojo.Core.TextEncoding.UTF8)
output.Write( EFResultat.Text.ToText )
output.Close
Catch e As Xojo.IO.IOException
AddErrorMessage "Unable to write file : " + e.Message + “(” + str(e.ErrorNumber) + “)”
AddErrorMessage "Reason : " + e.Reason
End Try
[/code]

Worked fine on all may test machine, but for users with problem ont their PC the result is (for both tempFolder and systemTempFolder) :

Unable to write file : This file could not be created (0)
Reason : This file could not be created

So, what’s the problem ?? What can I test now ? (I can’t file a report since I can’t reproduce the problem on my test machine).

I’ve just notice one thing … Both users have accents in their user name (One is Cdric, the other is Raphal). Perhaps this is the problem ?

Ok, I’ve created an account with accentuated characters in the name, launch the test app and … yep, it crashed with “This file could not be created (0)” …

Conclusion : Xojo.IO.TextOutputStream doesn’t like special characters… Not sure the feedback is about the same problem ? <https://xojo.com/issue/50240>

Is there a workaround ?

I’ve also tried to access the folder with 100% Xojo Framework, same problem

Dim outputFile As Xojo.IO.FolderItem outputFile = Xojo.IO.SpecialFolder.ApplicationSupport.Child(cPreferenceFolder.ToText).Child("temp")

Pascal, use ShellPath, instead of nativePath and your problem will go away.

Michel, ShellPath doesn’t work, and Xojo.IO doesn’t use NativePath or ShellPath and don’t work either.

Dim outputFile As Xojo.IO.FolderItem outputFile = Xojo.IO.SpecialFolder.ApplicationSupport.Child(cPreferenceFolder.ToText).Child("temp")

These feedback seems close to my problem … <https://xojo.com/issue/44726>

[quote=380238:@Pascal PLUCHON]'m having a strange problems using Xojo.net.HTTPSocket .
I download html pages and images from websites. To cache the files, I save them into a subfolder in SpecialFolder.Preferences for PC or SpecialFolder.ApplicationData for Mac

I call my Xojo.Net.HTTPSocket with something like this :[/quote]

Dim newPath As New Xojo.IO.FolderItem(PreferencesGetTemporaryFolder.NativePath.ToText) // PreferencesGetTemporaryFolder returns the classic path outputFile = newPath.Child( GenerateUUID.ToText ) // Create unique name for cache file ScriptRecherche.FileCache.Value( url ) = outputFile // Save the name in a dictionary key = url, value = FolderItem WRechercheInternet.XojoSocketListe.Send(methode.ToText, url.ToText, outputFile ) // Call the socket. methode = GET

Sorry, maybe I need to change my glasses, but I do see nativepath in the first line of the snippet you posted in your original post above.

On Windows, nativePath uses the user friendly form of the path, meaning with accented characters and spaces.

Shellpath uses the “short path”, which has no accented characters or spaces. It could alleviate the issue.

Ok, but he problem here is that Xojo.IO.SpecialFolder.ApplicationSupport contains accented characters, because the path is \Users\UserName\AppData\Roaming\, so if the UserName is “Cdric” for example, the path will be \Users\ Cdric\AppData\Roaming\ and an exception is raised :frowning:

(On Mac there’s no problem because you can’t create a user with forbidden characters like accents)

Ok, 'Ive juste tried

[code]Dim outputFile As Xojo.IO.FolderItem = Xojo.IO.SpecialFolder.Documents.Child(“1 - p.txt”)

Try
Dim output As Xojo.IO.TextOutputStream
output = Xojo.IO.TextOutputStream.Create(outputFile, Xojo.Core.TextEncoding.UTF8)
output.Write( “OK” )
output.Close
Catch e As Xojo.IO.IOException
MsgBox "Unable to write file : " + e.Message + “(” + str(e.ErrorNumber) + “)” + chr(13) + e.Reason
End Try
[/code]

On Mac I’ve got the file 1 - p.txt, and on PC I’ve got 1 - épé.txt

So, for me, something is broken with Xojo.IO

Sigh…

I tried to explain, but apparently you did not get it. Below the same path, shown two different ways in classic folderItem.

Here is native path :
C:\Users\mitch\Desktop\Cdric\mm.txt

Here is shell path, in Windows parlance, the short path :
C:\Users\mitch\Desktop\CDRIC~1\MM20F3~1.TXT

That is how you avoid the issue of accents and spaces.

Unfortunately, the pesky new framework does not seem to have anything like that. I would recommend going back to classic until the new framework is usable for non English speaking people…

BTW “On Mac I’ve got the file 1 - p.txt, and on PC I’ve got 1 - épé.txt” seems like on Windows you need some DefineEncoding. Could be the solution.

Sorry Michel, I’ve understood what you said, but unfortunately, using Xojo.Net means using Xojo.IO (with FileReceived event) which doesn’t work. That’s why I’ve tried many things to find what was wrong with my code (but yes, this was not my code who failed…)

So, as you say, I need to go back with classic FolderItem, but I need to use Xojo.Net, so I think that I’m going to use the PageReceived event and save the content with Classic FolderItem to get rid of this problem with accents, it’ll be faster than waiting for Xojo to fix Xojo.IO (but I’ll filled feedback reports for the bug I’ve found).