Jump to content

download file from HTML hyperLink


elGringo

Recommended Posts

hi,

looks like  simple task,

but cannot understand what am i doing wrong

 

having

<a href="UploadFolder/TestFile.txt">TestFile.txt</a>

on Server doing

procedure TUniServerModule.UniGUIServerModuleHTTPCommand(ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo; var Handled: Boolean);
var
  uriWinSlash: string;
  filepath: string;
  ext: string;
begin
//

  ext := ExtractFileExt(ARequestInfo.URI);
  if ext <> '' then
  begin
    uriWinSlash := StringReplace(ARequestInfo.URI, '//', '', [rfReplaceAll]);
    uriWinSlash := StringReplace(uriWinSlash, '/', '\', [rfReplaceAll]);
    if uriWinSlash[1] = '\' then
      uriWinSlash := uriWinSlash.Substring(1);
    filepath := UniServerModule.StartPath + uriWinSlash;
//  AResponseInfo.ServeFile(AContext, filepath);
    UniSession.SendFile(filepath); // << here mistake
//    Handled := true;
  end;

end;

but having mistakes in work of Server if even not downloading...

 

What's up? Any other ideas how to download file from HTML code?

Link to comment
Share on other sites

On simple HTTP Server i'am doing smth like this in OnCommandGet

 


    begin
      URIDecoded := TNetEncoding.URL.Decode(ARequestInfo.URI);
      IsWebPage := false;
      if IsFileOnServerByURI(URIDecoded, filepath, IsWebPage, WebPageContent) then
      begin
        AResponseInfo.ResponseNo := 200;
        if IsWebPage then
          AResponseInfo.ContentText := WebPageContent
        else
          AResponseInfo.ServeFile(AContext, filepath);
      end
      else
        AresponseInfo.ResponseNo := 404;
    end; 

But in UniGUI there is no AContext param...

Link to comment
Share on other sites

ok, Progress, i can do like this in HTML

<html>
 <a href="http://www.google.com" onclick="return myFunction();">Link</a>

<script>
function myFunction() {
    document.getElementById(".myDiv").style.flexGrow = "5"; 
}
</script>
</html>

next step is to learn to request through ajaxRequest from free HTML - is that possible?

Link to comment
Share on other sites

ok, my decision of my problem is 

<html>
 <a href="#" onclick="return myFunction();">Link</a>

<script>
function myFunction() {
alert('here');
ajaxRequest(MainForm.window, 'getFile', [ 'filePath=12345', 'param1=B' ]);
}
</script>
</html>

and on Server Side

  if SameText(EventName, 'getFile') then
  begin
    showmessage(Params.Values['filePath']);
  end;
Link to comment
Share on other sites

the only problem is i have js exception (see picture)

 

 my temporary decision is using timer - but it is not Ok decision !


        html := '<a href="#" onclick="return myFunction();">' + f.qFiles.FieldByName('fileName').AsString + '</a>' + //
          '<script>' + //
          'function myFunction() {' + //
          'replaceThis; ' + // <<<<<<<<<<<<<<<< !!! Here
//          'ajaxRequest(MainForm.window, ''getFile'', [ ''filePath=12345'', ''param1=B'']);' + //
          'return;' + //
          '}' + //
          '</script>';
      FFilePath := f.qFiles.FieldByName('filePath').AsString;
      UniSession.AddJS(UniHTMLMemo.JSName + '.execCmd(''InsertHTML'',''' + html + ''')');
      tReplace.Enabled := true;

and on timer

  tReplace.Tag := tReplace.Tag + 1;
//  inc(tReplace.Tag);
  if tReplace.Tag = 2 then
  begin
    UniHTMLMemo.Text := StringReplace(UniHTMLMemo.Text, 'replaceThis', 'ajaxRequest(MainForm.window, ''getFile'', [ ''filePath=' + FFilePath + ''', ''param1=B'']);', [rfReplaceAll]);
    tReplace.Enabled := false;
  end;

What could be the problem? 

 

You can easily reproduce the case by placing UniHTMLMemo and applying this code from previous message.

Link to comment
Share on other sites

Thank you delphiDude !!! I will watch your example.

 

below - decision from DD

procedure TMainForm.bTestClick(Sender: TObject);
var
  html: string;
begin
  html := '<a href="#" onclick="return myFunction();">Click Me</a>' + //
    '<script>' + //
    'function myFunction() {' + //
//    'replaceThis; ' + //
    'ajaxRequest(MainForm.window, "getFile", [ "filePath=12345", "param1=B"]);' + //
    'return;' + //
    '}' + //
    '</script>';
//      FFilePath := f.qFiles.FieldByName('filePath').AsString;
  UniSession.AddJS(UniHTMLMemo.JSName + '.execCmd(''InsertHTML'',''' + html + ''')');
end;
please try to use " instead of ' and '
Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...