Reading files in site public/*

I want to scan the files in the site’s public folder (that I import from) and get a directory. A bit like the windows code:
var
DirInfo : TSearchRec;
sFile : string;
iDosError : integer;
b
iDosError := FindFirst(‘.’,$3F,DirInfo);
while iDosError = 0 do
begin
sFile := DirInfo.Name;
do_something(DirInfo.Name);
iDosError := FindNext(DirInfo)
end

and then open it for read.
Also to write there, all using WIX javascript.

Thank you

Lawrence

The API does not support it (currently) afaik.

OK, let me simplify the question.

I believe javascript can open files (perhaps a bit like PHP). Is there such a function in WIX.
Also, are the public/*.js files accessible via a url?

Alternatively is the an API to upload or download these files?

Lawrence

Hi,
These files are not accessible via URL (unless you upload a txt file via media manger, then it’ll get an auto-URL, but you can’t set the url).
sometimes I use

// backend/serve-files.jsw
export function readFile(filePath){
//for example, filePath = 'backend/directoryname/file.txt'
 const fs = require('fs');
 return fs.readFileSync(require.resolve(filePath)).toString();
} 

to read the contents of a file in a known directory path.
You probably can use it in a jsw file
Just import the function to the front end and use it.
(with some adjustment you can read js files and not only txt files)
Maybe there’re simpler ways (I never needed it on the frontend, so I never tried).

is there then no way that in VELO I can read the contents of a file in public or even backend? The http_functions file are obviously public.

Why can’t you use the code I wrote yesterday? (maybe elaborate what you’re trying to do).

Anyway, you can combine the code I posted yesterday with an http-function if that what you need.

For example, I have code snippet that my partners implement on their non-Wix website, and the src points to my Wix http function url, once I get the request, I read the file content (with the code In posted yesterday) and return it as a javascript resource.

thanks for your help. I’ve got it to work.

I note that the snippet only “compiles” if it is in the backend folder, the requires would not work otherwise. Is there something special about the backend folder in that respect?

Can I also do a write to a file in the same idea? and even get a list of the files?

Thanks a lot. I did not see your code in the email notification that I got, so thanks.

Lawrence

Hi,

  1. I never tried to read a public file from the backend. Maybe it is possible, but you say you tested it. Right?
  2. Never tried write to a file. You can try based on:

That looks great, I will try that.
Where is the documentation for ‘fs’?
And, what is the magic of code in backend and should my code be here?

The fs is a built-in NodeJS module (and Wix backend side runs on NodeJS).
See docs:

Great, I wondered what NODEJS was. Now I understand. I didn’t come across that in the WIX API documentation!

Thanks
Lawrence