MT4 загрузить изображение на nodejs http сервер (с запросом) - PullRequest
0 голосов
/ 05 января 2020

Я пытаюсь загрузить изображение из metatrader на сервер nodejs. Это код mt4:

bool upload_image(string username, string password, string text, string filename, string filetype) {
   int res; // To receive the operation execution result
   char data[]; // Data array to send POST requests
   char file[]; // Read the image here
   string str = "username=" + username + "&password=" + password;
   string auth, sep = "-------Jyecslin9mp8RdKV"; // multipart data separator
   //--- A file is available, try to read it
   if (filename != NULL && filename != "") {
      res = FileOpen(filename, FILE_READ | FILE_BIN);
      if (res < 0) {
         Print("Error opening the file \"" + filename + "\"");
         return (false);
      }

      //--- Read file data
      if (FileReadArray(res, file) != FileSize(res)) {
         FileClose(res);
         Print("Error reading the file \"" + filename + "\"");
         return (false);
      }
      //---
      FileClose(res);

   }
   //--- Create the body of the POST request for authorization
   ArrayResize(data, StringToCharArray(str, data, 0, WHOLE_ARRAY, CP_UTF8) - 1);
   //--- Resetting error code
   ResetLastError();
   //--- Authorization request
   res = WebRequest("POST", "http://192.168.43.193/testAPI/login_mt", NULL, 0, data, data, str);
   //--- If authorization failed
   if (res != 200) {
      Print("Authorization error #" + (string) res + ", LastError=" + (string) GetLastError());
      return (false);
   }
   //--- Read the authorization cookie from the server response header
   res = StringFind(str, "Set-Cookie: auth=");
   //--- If cookie not found, return an error
   if (res < 0) {
      Print("Error, authorization data not found in the server response (check login/password)");
      return (false);
   }
   //--- Remember the authorization data and form the header for further requests
   auth = StringSubstr(str, res + 12);
   auth = "Cookie: " + StringSubstr(auth, 0, StringFind(auth, ";") + 1) + "\r\n";
   //--- If there is a data file, send it to the server
   if (ArraySize(file) != 0) {
      //--- Form the request body
      str = "--" + sep + "\r\n";
      str += "Content-Disposition: form-data; name=\"attachedFile_imagesLoader\"; filename=\"" + filename + "\"\r\n";
      str += "Content-Type: " + filetype + "\r\n\r\n";
      res = StringToCharArray(str, data);
      res += ArrayCopy(data, file, res - 1, 0);
      res += StringToCharArray("\r\n--" + sep + "--\r\n", data, res - 1);
      Print("Data: " + CharArrayToString(data));
      Print("File: " + CharArrayToString(file));
      Print("Header: " + str);
      ArrayResize(data, ArraySize(data) - 1);
      //--- Form the request header
      str = auth + "Content-Type: multipart/form-data; boundary=" + sep + "\r\n";
      //--- Reset error code
      ResetLastError();
      //--- Request to send an image file to the server

      res = WebRequest("POST", "http://192.168.43.193/testAPI/upload_img_mt", str, 0, data, data, str);
      //--- check the request result
      if (res != 200) {
         Print("Error sending a file to the server #" + (string) res + ", LastError=" + (string) GetLastError());
         return (false);
      }
      //--- Receive a link to the image uploaded to the server
      str = CharArrayToString(data);
      if (StringFind(str, "{\"Url\":\"") == 0) {
         res = StringFind(str, "\"", 8);
         filename = StringSubstr(str, 8, res - 8);
         //--- If file uploading fails, an empty link will be returned
         if (filename == "") {
            Print("File sending to server failed");
            return (false);
         }
      }
   }

   return true;
}

Это вывод скрипта:

uninit reason 0
Header: ---------Jyecslin9mp8RdKV Content-Disposition: form-data; name="attachedFile_imagesLoader"; filename="2020_1_3_23_58_59.png" Content-Type: image/png File: ‰PNG
Data: ---------Jyecslin9mp8RdKVnContent-Disposition: form-data; name="attachedFile_imagesLoader"; filename="2020_1_3_23_58_59.png" Content-Type: image/png ‰PNG
Files folder = C:\Users\alessio\AppData\Roaming\MetaQuotes\Terminal\51337600F56B69473E15EAFB8A7586B4\MQL4\Files
Alert: We've saved the screenshot 2020_1_3_23_58_59.png
initialized
Script sna USDJPY,M15: loaded successfully

Сервер реализован с express, таким образом:

router.post('/upload_img_mt', function(req, res, next){

    console.log("files: " +req.files);
    console.log("file: " +req.file);
    res.sendStatus(200);
});

Результат на консоли:

api@0.0.0 start / home / alessio / dev / реаги2 / узел API ./bin/www*1013*

POST / testAPI / login_mt 200 52,365 мс - 2

файлы: не определено

файл: не определено

POST / testAPI / upload_img_mt 200 2,198 мс - 2

Я не знаю, как правильно для управления файлом. Кто-нибудь может мне помочь?

...