|
Вернуть файл в POST запросе
#40111909
Ссылка:
Ссылка на сообщение:
Ссылка с названием темы:
Ссылка на профиль пользователя:
|
Участник
Откуда: Almaty, Kazakhstan
Сообщения: 2 520
|
|
пробую так
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95. 96. 97. 98. 99. 100. 101. 102. 103. 104. 105. 106. 107. 108. 109. 110. 111. 112. 113. 114. 115. 116. 117. 118. 119. 120.
function THTTPThrd.ProcessRequest(Request, URI: string): integer;
var
s,sql : string;
i: Integer;
FileName, ContentType: string;
ContentList : TStringList;
id : string;
jsRequest : TJSONObject;
command, username, password: string;
cmdcode, cmdresp: string;
Response : string;
begin
result := 504;
try
ContentList := TStringList.Create;
if request = 'GET' then begin
if (URI = '') or
(URI = '/') or
(URI = '\') then
begin
FileName := fDefault;
end else begin
FileName := ExtractFileDir(fDefault) + URI;
end;
if not (FileExists(FileName)) then begin
Headers.Add('Content-type: text/html; charset=utf-8');
FileName := '<title>Error</title>' +
'<h1><center><font color=red>' +
'Error 404: Script not found' +
'</font>' +
'</center></h1>';
ContentList.Add(FileName);
ContentList.SaveToStream(OutputData);
Result := 404;
Exit;
end; // if not (FileExists(FileName))
headers.Clear;
ContentType := GetContentType(FileName);
headers.Add(ContentType);
OutputData.LoadFromFile(FileName);
OutputData.Seek(0,0);
Result := 200;
Exit;
end; // if request = 'GET'
finally
FreeAndNil(ContentList);
end;
if request = 'POST' then begin
//if fPostSize > 0 then begin
id:= '[{E7854A1C-D822-44FA-A7A0-6368E9A3F47E}]';
try
FileName := '...\debug\resources\pages\message.html';
headers.Clear;
ContentType := GetContentType(FileName);
headers.Add(ContentType);
OutputData.LoadFromFile(FileName);
OutputData.Seek(0,0);
Result := 200;
Exit;
// test
fPostRequest := DecodeURL(fPostRequest);
jsRequest := TJSONObject.Create(fPostRequest);
username := jsRequest.getString('username');
password := jsRequest.getString('password');
command := jsRequest.getString('command');
Headers.Add('Content-type: application/json; charset=utf-8');
if (command = 'login') then begin
if (username = 'admin') and (password = 'test') then begin
Response := '{"status":200, "message":"OK", ';
Response := Response + '"session":"' + id + '"}';
end else begin
Response := '{"status":401, "message":"Unauthorized"}';
end;
end; // if (command = 'select')
if (command='sql') then begin
cmdcode := jsRequest.getString('cmdcode');
sql:= 'select sql_text, sql_type from tb_sql where sql_id=:sql_id';
DBQuery.Close;
DBQuery.Params.Clear;
DBQuery.SQL.Text:= sql;
DBQuery.FetchParams;
DBQuery.Params[0].AsInteger:= StrToInt(cmdcode);
DBQuery.Open;
sql := DBQuery.FindField('sql_text').AsString;
i := DBQuery.FindField('sql_type').AsInteger;
DBQuery.Close;
if (i = 5 ) or (i = 7) then begin
DBQuery.Close;
DBQuery.Params.Clear;
DBQuery.SQL.Text:= sql;
DBQuery.FetchParams;
DBQuery.Open;
DatasetToJson(DBQuery, cmdresp);
DBQuery.Close;
Response := '[' + cmdresp + ']';
end;
end;
except
on E:Exception do begin
s := E.Message;
Response := '{"status":400, "message":"' + s + '"}';
end;
end;
ContentList.Add(Response);
ContentList.SaveToStream(OutputData);
Result := 200;
Exit;
//end; // if fPostSize
end; // if request = 'POST'
end;
|
|
|