Я делаю приложение VCL Forms с TIdHTTPServer в основной форме и CommandGet процедуры IdHTTPServer:
procedure TForm6.IdHTTPServerCommandGet(AContext: TIdContext;
ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
var
indexStream: TFileStream;
path, indexPath: string;
begin
AResponseInfo.CharSet := 'UTF-8';
path := GetCurrentDir.Remove(GetCurrentDir.Length - Length('Win32\Debug'));
ARequestInfo.Document := path + 'scripts/script1.js';
ARequestInfo.Document := path + 'scripts/script2.js';
if pos('profile', ARequestInfo.UnparsedParams) > 0 then
begin
indexPath := path + 'index.html';
AResponseInfo.ContentStream := TFileStream.Create(indexPath, fmOpenReadWrite);
end;
end;
ИЗМЕНИТЬ
procedure TForm6.IdHTTPServerCommandGet(AContext: TIdContext;
ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
var
applicationDirectory: string;
begin
AResponseInfo.CharSet := 'UTF-8';
AResponseInfo.FreeContentStream := True;
applicationDirectory := ExtractFilePath(Application.ExeName)
.Remove(ExtractFilePath(Application.ExeName).Length -
Length('Win32\Debug') - 1);
AResponseInfo.ContentStream := TFileStream.Create(applicationDirectory +
'scripts/script1.js', fmOpenRead or fmShareDenyWrite);
AResponseInfo.ContentStream := TFileStream.Create(applicationDirectory +
'scripts/script2.js', fmOpenRead or fmShareDenyWrite);
if pos('profile&userName=', ARequestInfo.UnparsedParams) > 0 then
begin
AResponseInfo.ContentStream :=
TFileStream.Create(applicationDirectory + 'index.html', fmOpenRead);
end;
// other requests
end;
Вот содержимое index.html:
<html>
<head>
<title>Profile</title>
<script type="text/javascript" src="[app-path]/scripts/script1.js "></script>
<script type="text/javascript" src="[app-path]/scripts/script2.js "></script>
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="pragma" content="no-cache">
</head>
<body>
<!-- Page content -->
</body>
</html>
Когда я запускаю приложение и получаю доступ к http://localhost/?profile&userName=testUser
, оно работает нормально, но когда я запускаю приложение и набираю chrome-браузер другого компьютера http://{my-ip4-address}/?profile&userName=testUser
, я получаю два сообщения: «Не разрешено загружать локальный ресурс: файл:///{путь_приложения}/scripts/script1 .js», «Не разрешено загружать локальный ресурс: файл:///{путь_приложения}/scripts/script2.js. Содержимое HTML-страницы видно.
ИЗМЕНИТЬ
На основании комментариев и ответа index.html
изменено на
<html>
<head>
<title>Profile</title>
<script type="text/javascript" src="/scripts/script1.js "></script>
<script type="text/javascript" src="/scripts/script2.js "></script>
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="pragma" content="no-cache">
</head>
<body>
<!-- Page content -->
</body>
</html>
Редактируя свой код таким образом, я могу получить доступ к файлам, но проблема в том, что содержимое script1.js совпадает с содержимым script2.js. Любая идея, как правильно отправлять файлы.
http://{my-ip4-address}
ничего не знает о пути к файлу вашего компьютера. - person moskito-x   schedule 15.07.2014index.html
на сервер. - person moskito-x   schedule 15.07.2014index.html
, там вы загружаетеscript1.js
дважды. - person moskito-x   schedule 16.07.2014index.html
и файл, который вы показываете нам, различаются. - person moskito-x   schedule 16.07.2014