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)
- In the service rode define a method named FileUpload that accepts 2 parameters:
- name: Utf8String
- data: Binary
- 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