File upload using HTTP POST

I am wanting to have a ROSDK C# server service get a file that is selected by the user using a browser button, and uploaded. The normal RO SDK interface with the javascript is working, but my understanding is that this needs to be done using a standard form POST. It looks to me like the “PostMessage” component will probably do this, but how then do I tie it together? Do I create a new method in the RODL, called perhaps FileUpload? What parameters should be specified?

I’d much welcome pointers to understanding how this could be made to work. I’m normally a Delphi user, but am moving to C# with this project. Thanks.

Hello

Unfortunately POST message won’t help you much here as the data that is sent by the client browser doesn’t fit the data format it expects. Theoretically it is possible to implement the data accepting code using a http dispatcher component to the server side but there is a way easier approach.

So let’s assume you use RemObjects SDK for JavaScript and a more or less modern browser that supports FileAPI (for IE 9 there is a https://github.com/Jahdrien/FileReader component that should help to use the code below too)

  1. In the service rode define a method named FileUpload that accepts 2 parameters:
  • name: Utf8String
  • data: Binary
  1. Implement the service method in the service implementation class. Assume that 1st parameter will contain a remote file name and the second one - its content in a binary form.

For example

	public void FileUpload(string name, Binary data)
	{
		if (data != null)
			System.IO.File.WriteAllBytes(@"u:\data", data.ToArray());
		else
			System.IO.File.WriteAllBytes(@"u:\data", new Byte[0]);
	}

3.Regenerate the JS interface file for the service using ServiceBuilder
4. Add the following code to the client side JS app:
4.1. Html element to select a file

    <input type="file" id="files" name="files[]" />

4.2. JS handler code:

document.getElementById('files').addEventListener('change', handleFileSelect, false);

function handleFileSelect(evt) {
var files = evt.target.files; // FileList object

// Loop through the FileList and upload data
for (var i = 0, f; f = files[i]; i++) {
  var reader = new FileReader();

  reader.onload = (function(theFile) {
    return function(e) {
      // Send to the server
      AddToLog('Sending request...');
      Service.FileOperation(theFile.name, 
        //btoa(e.target.result),
        e.target.result,
     function(result) {
            AddLineToLog('  ok.');
            AddLineToLog('Done.');
     },
        function(msg, ex) {
            if (ex) {
                AddLineToLog(" Error: " + ex);
            }
            else
                AddLineToLog(msg.getErrorMessage());
        });
AddLineToLog('  ok.');
    };
  })(f);

  // Read in the file
  reader.readAsBinaryString(f);
}

}

Now you should be able to upload files to the server.

PS. Notice the commented line

        //btoa(e.target.result),

Unfortunately there is a minor bug in the RO SDK/JS in handling Binary data when JSON message is used. This is a temporary workaround, we’ll fix the issue ASAP

1 Like

Thanks - I will have to study that, but it is obviously possible.