Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Client
- procedure TMainForm.UploadFiles(const Files: TArray<String>);
- begin
- var lClient: IMVCRESTClient := TMVCRESTClient.Create;
- for var I := 1 to Length(Files) do
- begin
- lClient.AddFile('file' + I.ToString, Files[I-1], TMVCMediaType.APPLICATION_OCTET_STREAM);
- end;
- var lResp := lClient.Execute(httpPOST, ServerBaseURL + '/files');
- if not lResp.Success then
- begin
- ShowMessage(lResp.Content);
- end;
- end;
- // Server
- function TFilesController.UploadFile: IMVCResponse;
- var
- lFName: String;
- begin
- for var I := 0 to Context.Request.Files.Count - 1 do
- begin
- lFName := TPath.GetFileName(Context.Request.Files[I].FileName.Trim(['"']));
- if not TPath.HasValidFileNameChars(lFName, false) then
- begin
- raise EMVCException.Create
- (HTTP_STATUS.BadRequest, lFName + ' is not a valid filename for the hosting OS');
- end;
- TDirectory.CreateDirectory(GetUploadFolder);
- var lAbsoluteFileName := TPath.GetFullPath(TPath.Combine(GetUploadFolder, lFName));
- if TFile.Exists(lAbsoluteFileName) then
- begin
- try
- TFile.Delete(lAbsoluteFileName);
- except
- on E: Exception do
- begin
- raise EMVCException.Create
- (HTTP_STATUS.BadRequest, lFName + ' already present, cannot override - ' + E.Message);
- end;
- end;
- end;
- LogI('Uploading ' + lFName);
- var lFile := TFile.Create(lAbsoluteFileName);
- try
- lFile.CopyFrom(Context.Request.Files[I].Stream, 0);
- finally
- lFile.free;
- end;
- end;
- Result := OKResponse();
- end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement