Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 3 of 3 total
Thread ServerRequest and local path with space characters
Tue, May 12 2015 2:21 AMPermanent Link

Doc

I learned how to load a text file into a String variable using TServerRequest.
I put a text file in the same directory where my project html and js are and created the code below to access the file. This works if I load it from a server, e.g. localhost.
But if I try to load the file from my local file sytem it fails when the file path contains space characters.
Simply deactivating the web server and then starting the program leads to a fail because the Path (User\Documents\Elevate Web Bulder Projects\...) contains space characters.
No problem if I start the project from a folder with a path without blanks.
If I replace '%20' with ' ' in the URL (see code below) I can access the file using Firefox, but not with other browser.
Any suggestions? Is there a solution for that problem?

//-------------------------code------------------------------------  

 SLocation := window.location.href;

 if RPos ('/', SLocation) = 0 then
 SURLDatei := Copy (SLocation, 1, RPos ('\', SLocation))
  else
 SURLDatei := Copy (SLocation, 1, RPos ('/', SLocation));
 
 SURLDatei := SURLDatei + 'MyText.txt';

 if Pos ('file:', SURLDatei) > 0 then
  begin
   SURLDatei := StrReplace (SURLDatei, '%20', ' ', true, false);
  end;

 ServerRequest1.URL := SURLDatei;
 ServerRequest1.Execute;

//-------------------------------------------------------------

function RPos(Substr: string; S: string): Integer;
//ermittelt das letzte Vorkommen von Substr im String S
var
 i: Integer;
begin
 Result := 0;
 if ((Length(S) > 0) and (Length(Substr) > 0)) then
   if (Length(S) >= Length(Substr)) then
     for i:= (Length(S) - Length(Substr)) downto 1 do
       if (Copy(S, i, Length(Substr)) = Substr) then
       begin
         Result := i;
         Exit;
    
Tue, May 12 2015 4:47 AMPermanent Link

Matthew Jones

Doc wrote:

> If I replace '%20' with ' ' in the URL (see code below) I can access
> the file using Firefox, but not with other browser.  Any suggestions?
> Is there a solution for that problem?

The simple answer is that you cannot do this. No browser should allow
you to. The reason is that an application (javascript) accessing files
on your hard disk is a security risk. "Hey, try my little game" which
while you play space invaders, is trawling your hard disk for password
files and other such data and sending it to my server.

Basically, data needs to come from the server. Only if you do a file
selector and ask for a data file to upload should a browser allow
access. I suspect FireFox is allowing it because you have localhost set
for a trusted zone or something.

--

Matthew Jones
Tue, May 12 2015 6:06 AMPermanent Link

Doc

Thank you.
You are right. It only works with firefox, but on different PCs. Seems not to be a special configuration.
Image