Search code examples
delphirestuploaddatasnap

How to update file in datasnap rest server with http?


my question is as tittle.I have a datasnap rest server,I want upload file with java or java script.How do? (best have sample code) thank your very much!


Solution

  • I've once tried to pass a stream to a REST call, but eventually resorted to uploading with a direct WebBroker request to a page... which is easier to manage. That's not trivial either, as you'll have to manage the multi-part request data that you receive when you use a form to let the user pick the file to upload.

    This is some of the code I'm using in WebModuleBeforeDispatch:

    type TMyIdHTTPAppRequest = class (TIdHTTPAppRequest);

        haReq := TMyIdHTTPAppRequest(Request);
        fname := IntToStr (haReq.ContentLength);
        multi := TMultipartRequestFiles.Create(haReq.FRequestInfo,
          haReq.content);
    
        if (multi.Count > 0) and Assigned (multi.Items[0]) then
        begin
         theFile := TFileStream.Create(ServerConf['fileupload:tempdir'] + folder + '\' +
            multi.Items[0].FileName, fmCreate);
          multi.Items[0].Stream.Position := 0;
          TheFile.CopyFrom(multi.Items[0].Stream, multi.Items[0].Stream.Size);
          theFile.Free;
    

    However it is based on a custom TMultipartRequestFiles that I have, trying to clean it up and load it as part of my Relax open source extension to Delphi datasnap.

    Any other solution to manage file uploads? I guess there should be something ready to use in Indy, I wrote that class many years ago.