IntraWeb FileUploader Demo |
unit Unit1;
interface
uses
Classes, SysUtils, IWAppForm, IWApplication, IWColor, IWTypes,
IWCompFileUploader, Vcl.Controls, IWVCLBaseControl, IWBaseControl,
type
TIWForm1 = class(TIWAppForm)
IWFileUploader1: TIWFileUploader;
IWFileUploader2: TIWFileUploader;
IWFileUploader3: TIWFileUploader;
ClientDataSet1: TClientDataSet;
ClientDataSet1ClientDataSet1FileName: TStringField;
ClientDataSet1ClientDataSet1FileSize: TIntegerField;
ClientDataSet1ClientDataSet1FileContent: TBlobField;
procedure IWButton1Click(Sender: TObject);
procedure IWFileUploader1AsyncUploadCompleted(Sender: TObject; var DestPath,
FileName: string; var SaveFile, Overwrite: Boolean);
procedure IWFileUploader2AsyncUploadCompleted(Sender: TObject; var DestPath,
FileName: string; var SaveFile, Overwrite: Boolean);
procedure IWFileUploader3AsyncUploadCompleted(Sender: TObject; var DestPath,
FileName: string; var SaveFile, Overwrite: Boolean);
procedure IWAppFormCreate(Sender: TObject);
public
end;
implementation
{$R *.dfm}
procedure TIWForm1.IWAppFormCreate(Sender: TObject);
begin
// Create the DataSet so we can use it later
ClientDataSet1.CreateDataSet;
end;
procedure TIWForm1.IWButton1Click(Sender: TObject);
begin
WebApplication.ShowMessage(WebApplication.ApplicationPath);
end;
procedure TIWForm1.IWFileUploader1AsyncUploadCompleted(Sender: TObject;
var DestPath, FileName: string; var SaveFile, Overwrite: Boolean);
var
CurDir: string;
begin
// get the app path
CurDir := WebApplication.ApplicationPath+'\myUploads\';
// save in the same application directory, with the same name of the original file. Overwrite if it already exists.
IWFileUploader1.SaveToFile(FileName, CurDir + FileName, True);
// Inform IWFileUploader that we are taking care of file saving ourselves
SaveFile := False;
end;
procedure TIWForm1.IWFileUploader2AsyncUploadCompleted(Sender: TObject;
var DestPath, FileName: string; var SaveFile, Overwrite: Boolean);
var
CurDir: string;
FileName1:string;
begin
// get the app path
CurDir := WebApplication.ApplicationPath+'\myUploads\';
// here we just inform the new name for the file. IWFileUploader will take care of the rest.
// The file will be saved inside the user cache directory (WebApplication.UserCacheDir)
FileName1 := 'MyUploadedFile.dat';
IWFileUploader2.SaveToFile(FileName, CurDir + FileName1, True);
end;
procedure TIWForm1.IWFileUploader3AsyncUploadCompleted(Sender: TObject;
var DestPath, FileName: string; var SaveFile, Overwrite: Boolean);
var
MS: TMemoryStream;
begin
// Create any TStream descendant
MS := TMemoryStream.Create;
try
// Save the file content to that stream
IWFileUploader3.SaveToStream(FileName, MS);
// inform IWFileUploader that we are handling it ourselves. No need to save the file
SaveFile := False;
// do whatever you want here with the TStream containing the file. For instance, save to the ClientDataSet
with ClientDataSet1 do begin
Append;
FieldByName('FileName').AsString := FileName;
FieldByName('FileSize').AsInteger := MS.Size;
// set to the start of the Stream
MS.Position := 0;
// save to the blob field
TBlobField(FieldByName('FileContent')).LoadFromStream(MS);
Post;
end;
finally
// Release the stream
MS.Free;
end;
end;
initialization
TIWForm1.SetAsMainForm;
end.
沒有留言:
張貼留言