File upload to Project server via desktop UI?

Find and share HowTos to various installations / configurations!
6 posts • Page 1 of 1
daniel.ponader
Posts:34
Joined: Tue Jan 10, 2012 1:40 pm

File upload to Project server via desktop UI?

Post by daniel.ponader »

Hello together,

is there any possibility to upload files into the data folder from a remote Desktop UI? We are using a server PC without Display, for running the UI we are using mobile PCs with Desktop UI. Now there is the requirement, to upload files (different documents e.g. checklists, maintenance plans, etc.) to the project without having direct access to the server. The download of available documents from data path into the desktop UI via getPath works fine. But into the other direction (copyfile into data folder) the documents are only saved into the cache folder and lost after closing the connection..

Thanks a lot

Best regards
Daniel

mkoller
Posts:741
Joined: Fri Sep 17, 2010 9:03 am

Re: File upload to Project server via desktop UI?

Post by mkoller »

you can use netPost() on the client in combination with httpConnect() and httpSaveFilesFromUpload() in the server side http script.

daniel.ponader
Posts:34
Joined: Tue Jan 10, 2012 1:40 pm

Re: File upload to Project server via desktop UI?

Post by daniel.ponader »

Hello Martin,

thanks for the quick response:

so what I have to do is to add a httpConnect like this on the server side:


httpConnect("fileupload","/fileupload");


with a callback function like this (it's from the help):

fileupload(blob content, string user, string ip, dyn_string headernames, dyn_string headervalues, int connIdx)
{
string contentType = httpGetHeader(connIdx, "Content-Type");
int pos = strpos(contentType, "boundary=");
if (pos >= 0)
{
string boundary = substr(contentType, pos + 9);
mapping result;

int retval = httpSaveFilesFromUpload(content, boundary, "D:\\\\Test\\\\", result);

DebugN("return", retval);
DebugN("result", result);

}

fine so far, but now I tried to configure the netPost() on the client (for testing on the same machine):

main
{
mapping m;
file f;
blob cont;
int read;
f = fopen(DATA_PATH+"bda/TEST.pdf", "rb");
read = blobRead(cont, 1000000000, f);
fclose(f);
netPost("http://localhost:80/fileupload",
makeMapping("content", cont,
"headers", makeMapping("Content-Type", "multipart/form-data"),
"ignoreSslErrors","")
,m);
}

The result is that the content is always empty, I also tried to use string instead of a blob, result is the same.
Were is my mistake?

Thanks and best regards

mkoller
Posts:741
Joined: Fri Sep 17, 2010 9:03 am

Re: File upload to Project server via desktop UI?

Post by mkoller »

A multipart message is a bit more complex to create, and I sadly found out that our httpSaveFilesFromUpload() function does not use Content-Transfer-Encoding of the parts so you can not transfer a binary file content as base64 encoded. Therefore a bit of a trick has to be used and we let httpSaveFilesFromUpload() not save the file directly but just use the mapping it fills with the data and do the bas64decode on our own and then save the file:

Server code:

Code: Select all

#uses "CtrlHTTP"

main()
{
  httpServer(false, 8080, 8079);
  
  httpConnect("fileupload","/fileupload"); 
}

fileupload(blob content, string user, string ip, dyn_string headernames, dyn_string headervalues, int connIdx)
{
  string contentType = httpGetHeader(connIdx, "Content-Type");
  int pos = strpos(contentType, "boundary="); 
  if (pos >= 0)
  {
    string boundary = substr(contentType, pos + 9); 
    mapping result;

    int retval = httpSaveFilesFromUpload(content, boundary, "", result);

    DebugN("return", retval);
    DebugN("result", result);
    
    blob data;
    base64Decode(result["file"]["content"], data);
    file fd = fopen(DATA_PATH + "upload/" + result["file"]["X-filename"], "wb");
    blobWrite(data, fd);
    fclose(fd);
  }
}
client code:

Code: Select all

main(mapping event)
{
  mapping m;
  blob cont = "010203040506070809";
  
  const string boundary = createUuid();
  string data = base64Encode(cont);
  string part = "--" + boundary + "\\r\\n" +
                "Content-Transfer-Encoding: base64\\r\\n"
                "Content-Disposition: attachment; name=\\"file\\" \\r\\n"
                "X-filename: test.blob\\r\\n"
                "\\r\\n" + data + "\\r\\n"
                "--" + boundary + "--";
  
  netPost("http://localhost:8080/fileupload",
          makeMapping("content", part,
                      "headers", makeMapping("Content-Type", "multipart/form-data; boundary=" + boundary),
                      "ignoreSslErrors",""),
          m);
}

mkoller
Posts:741
Joined: Fri Sep 17, 2010 9:03 am

Re: File upload to Project server via desktop UI?

Post by mkoller »

ok, so the previous code was more about "how to get a multipart netPost working", but for a much simpler and more efficient way to transfer a single file, better use the following:

Server code:

Code: Select all

#uses "CtrlHTTP"

main()
{
  httpServer(false, 8080, 8079);
  
  httpConnect("fileupload","/fileupload"); 
}

fileupload(blob content, string user, string ip, dyn_string headernames, dyn_string headervalues, int connIdx)
{
  string fileName =  httpGetHeader(connIdx, "X-filename");
  file fd = fopen(DATA_PATH + "upload/" + fileName, "wb");
  blobWrite(content, fd);
  fclose(fd);
}
client code:

Code: Select all

main(mapping event)
{
  string fileName = "fileToUpload";

  netPost("http://localhost:8080/fileupload",
          makeMapping("source", DATA_PATH + fileName,
                      "headers", makeMapping("Content-Type", "application/octet-stream",
                                             "X-filename", fileName)));
}

daniel.ponader
Posts:34
Joined: Tue Jan 10, 2012 1:40 pm

Re: File upload to Project server via desktop UI?

Post by daniel.ponader »

Hello Martin,
thanks a lot for this hint. This is exactly what I've been looking for.

Best regards
Daniel

6 posts • Page 1 of 1