demoreadfileinput.lpr 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. program demoreadfileinput;
  2. uses
  3. Web,JS;
  4. const
  5. DownloadFileName = 'result.txt';
  6. var
  7. GInput: TJSHTMLInputElement;
  8. Goutput: TJSHTMLAnchorElement;
  9. LReader: TJSFileReader;
  10. LFile: TJSHTMLFile;
  11. LFileContent: String;
  12. begin
  13. GInput := TJSHTMLInputElement(Document.GetElementByID('input'));
  14. Goutput := TJSHTMLAnchorElement(Document.GetElementByID('output'));
  15. GInput.OnChange := function (AFileInputChangeEvent: TEventListenerEvent): Boolean
  16. begin
  17. LFile := GInput.Files[0];
  18. LReader := TJSFileReader.New;
  19. LReader.OnLoad := function (AFileLoadEvent: TEventListenerEvent): Boolean
  20. begin
  21. LFileContent := String(TJSFileReader(AFileLoadEvent.Target).Result);
  22. // begin edit
  23. LFileContent := '<pre>' + LineEnding + LFileContent + LineEnding + '</pre>';
  24. // end edit
  25. // need a way to assign back modified LFileContent to LFile or create a new TJS(HTMLFile|Blob) with LFileContent as its content
  26. // the Web API standard provides the way as a constructor parameter, but is missing from the declaration
  27. LFile := TJSHTMLFile.New(TJSString.New(LFileContent), DownloadFileName);
  28. Goutput.HRef := TJSURL.createObjectURL(LFile);
  29. Goutput.Download := DownloadFileName;
  30. Goutput.Click;
  31. TJSURL.revokeObjectURL(Goutput.HRef);
  32. end;
  33. LReader.ReadAsText(LFile);
  34. end;
  35. end.