Правя VCL Forms Application с 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
, получавам две съобщения: „Не е разрешено да зарежда локален ресурс: file:///{app_path}/scripts/script1 .js', 'Не е разрешено зареждането на локален ресурс: file:///{app_path}/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