help files
Z505 | PasWiki | FUQ | Search | Main Docs | API Guide



Notes

Below is a simple demo showing how to retrieve a file from a clients computer over to the server. Information retrieved is the actual file content, the type of file, the size of file, and more.

Code

Simple Example

if IsUpFile('NameOfWidget') then
begin
  // note: doc root path can be retreived with SERV.docroot() in pwenvvar.pas
  SaveUpFile('NameOfWidget', 'path/to/save');
end;

More Demonstration

program upload; {$mode objfpc} {$H+}
uses
  pwinit,           // only in 1.7.x
  pwmain,           // note: "pwu" prefix in v1.6.X
  compactsysutils,  // or use sysutils.pas
  pwenvvar;         // note: "pwu" prefix in v1.6.x

// Note: Use HTML templates, FileOut(),  OutF() or OutA() calls, or the rapid html 
// units if you want the source to be easier to manage than below demo with hardcoded
// HTML. It is here for simplicity and so that there are no dependencies.

const FUPL = 'fupl';

procedure showform;
begin
  out(
  '<FORM METHOD=POST ACTION="" enctype="multipart/form-data">' +
      '<input name='+FUPL+' type=file size=20> ' +
      '<br><br>'+
      '<input type=submit>' +
  '</FORM>' +
  '<p>Click to upload file and get info</p> ');
end;

// "break" also available in the pwhtmw.pas unit
procedure break; 
begin  out('<br>');
end;

procedure outbr(s: string); 
begin  out(s); break;
end;

// note: make sure public_html/temp-tests/upfiles/ exists on server
procedure processupfile;
var
  fpath,               
  absolutefpath: string; // full path to save to
begin
  fpath:= '/temp-tests/upfiles/' + getupfilename(FUPL);
  absolutefpath:= cgienvvar.docroot() + fpath;  
  outbr('Upfile name: ' + getupfilename(FUPL));
  outbr('Upfile type: ' + getupfiletype(FUPL));
  outbr('Upfile size: ' + inttostr(getupfilesize(FUPL)) );
  if saveupfile(FUPL, absolutefpath) then
  begin
    outbr('Saved to: <a href="'+fpath+'">'+ fpath +'</a>');
    outbr('Absolute path: ' + absolutefpath);
  end else 
  begin
    outbr('Error saving file. Directory may not exist.');
    outbr('Tried to save to: ' + absolutefpath);
  end;
end;

begin
  if isupfile(FUPL) then processupfile else showform;
end.





lufdoc, Powtils, fpc, freepascal, delphi, kylix, c/c++, mysql, cgi web framework docs, Z505