jArchi Uploading image to server - corrupted image

Started by Xavier Mayeur, February 22, 2019, 13:58:10 PM

Previous topic - Next topic

Xavier Mayeur

February 22, 2019, 13:58:10 PM Last Edit: February 22, 2019, 21:22:05 PM by Xavier Mayeur
Hello,


I am struggling trying to upload a PNG image to a web site, in order to publish my architecture developed wit Archi.


The POST actually works and the the image is uploaded (HTTP return code 200).
Unfortunately, the uploaded image binary code does not match the original file.
I suspect a character set conversion issue, but I do not succeed to solve it, being a real newbie in Java Script.


Here a snippet of the code. Does it trigger some suggestion to one of you ?


Thanks


Xavier


--------------------------------------------------------------------------------------------------------------------------



function httpSendImage(method, theUrl, files,  headers) {


    method = method || 'POST';


    lineEnd = "\r\n";
    twoHyphens = "--";
    boundary = "***232404jkg4220957934FW**";
   var   existingFileName='';


    maxBufferSize = 1 * 1024 * 1024;


    // Post Client request     
    try {
        var con = new java.net.URL(theUrl).openConnection();
        con.doOutput=true;
        con.doInput=true;
        con.useCaches=false;
        con.requestMethod = method;
       
        con.setRequestProperty("Connection", "Keep-Alive");
        con.setRequestProperty("Content-Type","multipart/form-data;boundary=" + boundary);
      
      var filesData ='';
      if (files != undefined) {
         for (var prop in files) {
            if (prop === 'filename') {
               existingFileName = files[prop];
               fileparts = existingFileName.split("\\");
               files[prop] = fileparts[fileparts.length-1];
            }
            filesData += prop + '=\"' +  files[prop] + '\"; ';
         }
         filesData = filesData.substring(0, filesData.length-2);
        }   


        if (headers != undefined) {
            for (var prop in headers) {
                con.setRequestProperty(prop ,headers[prop]);
            }
        }


        if (authorization != null) {con.setRequestProperty("Authorization", authorization);}


        var dos = new java.io.DataOutputStream(con.outputStream);
      var buf = Java.type('byte[]');
        dos.writeBytes(twoHyphens + boundary + lineEnd);
        dos.writeBytes("Content-Disposition: form-data; name=\"file\"; " + filesData  + "\nContent-Type: image/png; charset=utf-8" + lineEnd);
        dos.writeBytes(lineEnd);


        // write bytes into form...


      buf = readFile(existingFileName);
      // console.log('buf length: ' + buf.length);
      dos.writeBytes(buf);
        // send multipart form data necesssary after file data...

        dos.writeBytes(lineEnd);
        dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

        // close streams
        dos.flush();
        dos.close();


        return asResponse(con);


    }
    catch(err) {
        console.log("Error sending file: " + err.message);
    }


}


Phil Beauvoir

As this is, I think, a more generalised Java/JavaScript issue you may get more help on something like Stack Overflow or similar forums. Sorry I can't help with this.
If you value and use Archi please consider making a donation! https://www.archimatetool.com/donate