给你一套完整的例子! Delphi代码+CGI
program Cgitest;
{$APPTYPE CONSOLE}
uses
Windows, Sysutils, Filectrl;
{$R *.RES}
Var
TempURL, // Temp URLencoded string
RawURL, // Raw input string URLencoded
DecURL: String; // Decoded input string
Index: Integer; // Global index to URL string
Temp,
Filepath, // Path of Output file
Outname: String; // Path and file name of output file
Tfile: Text; // Output file
// Get a single character from TempURL string
Function GetURLchar: Char;
Begin
If Index<=Length(TempURL) then // If index is ok
Result := TempURL[Index] // Return a char
else
Result := ' '; // Return space if error
Inc(Index);
end;
// Converts a hex character (0 to F) to an integer (0 to 15)
Function Hex2Int(Inch: Char): Integer;
Begin
Result := 0; // Return 0 if invalid character
If Inch in ['A'..'F'] then
Result := Ord(Inch)-$37;
If Inch in ['a'..'f'] then
Result := Ord(Inch)-$57;
If Inch in ['0'..'9'] then
Result := Ord(Inch)-$30;
end;
// Converts a URLencoded string to a normal string
Function DecodeURL(Instr: String): String;
Var
S: String;
X: Integer;
C,N1,N2: Char;
Begin
If Instr='' then exit;
S := '';
TempURL := Instr; // Copy URLencoded string
Index := 1; // Reset string index
Repeat
C := GetURLchar; // Get next character
If C='%' then // If it's a hex esc char
Begin
N1 := GetURLchar; // Get first digit
N2 := GetURLchar; // Get second digit
X := (Hex2Int(N1)*16)+Hex2Int(N2); // Convert to integer
S := S+Chr(X); // Add character
end else
If C='+' then // If + then convert to space
S := S+' ' else
S := S+C; // Just add character
Until C=' '; // Until no more in string
Result := S;
end;
Procedure Findfilename(Cstring: String);
Var
P, // Index of start of param/value pair
E, // Index of end of param/value pair
L, // Length of Cstring
EQ: Integer; // Position of equals sign
NV, // Name value pair
Name,
Value,
S: String; // Substring which gradually gets shorter as we parse
Begin
L := Length(Cstring);
P := 0;
S := Cstring; // Set up Temp string for 1st pass
Repeat
S := Copy(S,P+1,L); // Get substring from &+1
E := Pos('&',S); // Find next &
If E>0 then // If another & found
NV := Copy(S,1,E-1) // Get name/value pair
else // If no more after this
NV := Copy(S,1,L); // Get last one
EQ := Pos('=',NV); // Find =
If EQ>0 then // If it's there
Begin
Name := Copy(NV,1,EQ-1); // Extract name
Value := Copy(NV,EQ+1,L); // Extract value
If Uppercase(Name)='FILENAME' then // If we've found it
Begin
Outname := Value; // Set output file name
exit; // Dont check any more
end;
end;
If E>0 then P := E; // If more to do set start of next one
Until E=0; // Until no more &'s
end;
// Write all URLencoded name/value pairs in Cstring to a file
Procedure Writefile(Cstring: String);
Var
P, // Index of start of param/value pair
E, // Index of end of param/value pair
L, // Length of Cstring
EQ: Integer; // Position of equals sign
NV, // Name value pair
Name,
Value,
Tmp,
S: String; // Substring which gradually gets shorter as we parse
Begin
L := Length(Cstring);
P := 0;
S := Cstring; // Set up Temp string for 1st pass
Repeat
S := Copy(S,P+1,L); // Get substring from &+1
E := Pos('&',S); // Find next &
If E>0 then // If another & found
NV := Copy(S,1,E-1) // Get name/value pair
else // If no more after this
NV := Copy(S,1,L); // Get last one
EQ := Pos('=',NV); // Find =
If EQ>0 then // If it's there
Begin
Name := Copy(NV,1,EQ-1); // Extract name
Value := Copy(NV,EQ+1,L); // Extract value
If Uppercase(Name)<>'FILENAME' then // If it's anything but filename
Begin
If Value<>'' then
Tmp := DecodeURL(Value) // Decode value string
else
Tmp := '';
Writeln(Name+': '+Tmp+'<br>'); // Write to stdout
Writeln(Tfile, Name+': '+Tmp); // and to file
end;
end;
If E>0 then P := E; // If more to do set start of next one
Until E=0; // Until no more &'s
end;
// Start of main program
begin
Readln(RawURL); // Read the URLencoded string from Web server
//RawURL:='Filename=C%3A%5Cwww%5CIntranet%5CTest%5CTestfile.txt&Name=Testname&damn=&Comments=Test+Comment';
DecURL := DecodeURL(RawURL); // Convert to "normal" text
Outname := ''; // Reset out file name
Findfilename(DecURL); // Try to find file name
If Outname<>'' then // If we have a filename
Begin
Filepath := ExtractFiledir(Outname); // Get path of file
If DirectoryExists(Filepath) then // If folder exists
Begin
If Fileexists(Filepath+'/Head.htm') then // If header file
Begin
Assignfile(Tfile, Filepath+'/Head.htm'); // Assign the file
Reset(Tfile); // Reset it
While not Eof(Tfile) do // For whole file
Begin
Readln(Tfile, Temp); // Read a line
Writeln(Temp); // Write to stdout
end;
Close(Tfile);
end;
// Writeln('Raw URL was '+RawURL); // For debugging
// Writeln('Decoded URL was '+DecURL);
Assignfile(Tfile, Outname); // Assign file name
If Fileexists(Outname) then // If it exists
Append(Tfile) // Append to it
else // Otherwise create a new one
Rewrite(Tfile);
Writefile(RawURL); // Write all the values
Writeln(Tfile, '----------------------------------------');
Closefile(Tfile); // Close the file
Writeln('<br><b>Data successfully written to '+
Outname+'</b>');
If Fileexists(Filepath+'/Tail.htm') then // If header file
Begin
Assignfile(Tfile, Filepath+'/Tail.htm'); // Assign the file
Reset(Tfile); // Reset it
While not Eof(Tfile) do // For whole file
Begin
Readln(Tfile, Temp); // Read a line
Writeln(Temp); // Write to stdout
end;
Close(Tfile);
end;
end else
Writeln('<br><b>ERROR - Invalid file folder '+
Filepath+'</b>');
end;
end.