Upload a file via HTTP

313 views
Skip to first unread message

Franz

unread,
Jul 21, 2021, 2:26:32 AM7/21/21
to Harbour Users

I would like to upload a file to my website directly from Harbour.
I tried this code found on the newsgroup but apparently it seems to work but I can't find the file on the server.
Where is the problem?

Harbour program

REQUEST HB_GT_WIN
REQUEST HB_CODEPAGE_PTISO

PROCEDURE Main( cFile, cUrl )

   LOCAL oHttp, cResponse, hResponse, cFileHash, nFileSize

   setmode(25,80)

IF Empty( cFile ) .OR. Empty ( cUrl )
   ? "Usage FileUpload <cFileName> <cUrl>"
   inkey(0)
   QUIT
ENDIF

cFileHash := hb_MD5File( cFile ) // Just for simplicity, avoid MD5
? "Local file hash (MD5)...", cFileHash
nFileSize := hb_FSize( cFile )
? "Local file size.........", nFileSize

oHttp := TIPClientHTTP():New( cUrl, .T. )

IF oHttp:open()
    oHttp:Attach("files", cFile, "application/octet-stream")
    oHttp:PostMultiPart()

    ? "TIP Client HTTP Status", oHttp:lastErrorMessage()

    cResponse := oHttp:ReadAll()

    IF .NOT. Empty( cResponse )
        hb_jsonDecode(cResponse, @hResponse)
        ? "Remote file hash (MD5).:", hResponse["filehash"]
        ? "Remote file size.......:", hResponse["filesize"]

        ? "Local and remote sizes are equals?", nFileSize == hResponse["filesize"]
        ? "Local and remote hashs are equals?", cFileHash == hResponse["filehash"]
        ? ""
        ? cResponse
    ENDIF

ELSE
   ? "Error:", oHttp:lastErrorMessage()
ENDIF

?
? "-------------"
? "    END      "
? "-------------"
inkey(0)

oHttp:close()

Script PHP sul server

 <?php

error_reporting(E_ALL);

if(isset($_FILES))
{
    if($_FILES["files"]["error"] > 0)
    {
        echo "Error: " . $_FILES["files"]["error"] . "<br/>";
    }
    else
    {
        echo json_encode(array("filename" => $_FILES["files"]["name"], "filetype" => $_FILES["files"]["type"], "filesize" => $_FILES["files"]["size"], "filehash" => md5_file($_FILES["files"]["tmp_name"])));

    }
}

?>


Diego Pego

unread,
Jul 21, 2021, 9:52:59 PM7/21/21
to Harbour Users
Go for hbcurl lib.

Guillermo Varona Silupú

unread,
Jul 21, 2021, 11:25:54 PM7/21/21
to harbou...@googlegroups.com

[ENG by Google]

Hello, I have tried some things with CURL (see this thread: https://groups.google.com/g/harbour-users/c/iZ9lvcvy-ns), and they go well, the problem is that it asks me for several DLL's, and when I paste them in the EXE folder, the program does not work. Is there a way to make a self-contained EXE that uses CURL?

[Spanish]

Hola, he probado algunas cosas con CURL (ver éste hilo: https://groups.google.com/g/harbour-users/c/iZ9lvcvy-ns), y van bien, el problema es que me pide varias DLL's, y cuando las pego en la carpeta del EXE, el programa no funciona. ¿Hay alguna forma de hacer un EXE autocontenido que use CURL?.

BR

GVARONAS

--
--
You received this message because you are subscribed to the Google
Groups "Harbour Users" group.
Unsubscribe: harbour-user...@googlegroups.com
Web: http://groups.google.com/group/harbour-users

---
You received this message because you are subscribed to the Google Groups "Harbour Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to harbour-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/harbour-users/432d7796-2764-4b4a-8e27-7f986efdb50dn%40googlegroups.com.

Franz

unread,
Jul 22, 2021, 2:53:17 AM7/22/21
to Harbour Users
Any samples ?

Shaji Thomas

unread,
Jul 22, 2021, 3:05:55 AM7/22/21
to Harbour Users
Is there any method to use HBCURL without LIBs (included in EXE) ,  just like adding HBC files
I am using Harbour Binary download, not the  source

hmpaquito

unread,
Jul 22, 2021, 5:43:23 AM7/22/21
to Harbour Users
Use CUrl.exe command line

Guillermo Varona Silupú

unread,
Jul 24, 2021, 12:57:23 PM7/24/21
to harbou...@googlegroups.com
El 22/07/2021 a las 04:43, hmpaquito escribió:
Use CUrl.exe command line

hmpaquito:
El jueves, 22 de julio de 2021 a la(s) 04:43:23 UTC-5, hmpaquito escribió:
Use CUrl.exe command line

[Google] 
Thanks for the suggestion, I use CURL.EXE and everything is fine, the only drawback is that when I launch the command with HB_RUN or HB_PROCESSRUN a console window appears in the foreground and after executing the command it disappears. Is there a way to prevent the output of this console window?

[Spanish]
Gracias por la sugerencia, uso CURL.EXE y todo bien, el único inconveniente es que cuando lanzo el comando con HB_RUN o HB_PROCESSRUN sale una ventana de la consola en primer plano y luego de ejecutarse el comando desaparece. ¿Habrá alguna forma de evitar la salida de ésta ventana de consola?

BR
GVARONAS

Pierpaolo Martinello

unread,
Jul 25, 2021, 6:07:19 AM7/25/21
to Harbour Users
Hi Guillermo,
you can adapt my code to Launch curl and capture the resolved without opening console windows

*-----------------------------------------------------------------------------*
FUNCTION Scan( img )
*-----------------------------------------------------------------------------*
   LOCAL cCommand := "zbarimg.exe --raw -q " + ["]+img+["]
   LOCAL hProcess, cResult
   LOCAL hStdOut, hStderr, nState, nBytes
   LOCAL cBuff := Space( 1024 )

   hProcess := hb_ProcessOpen( cCommand, NIL, @hStdOut, @hStdErr, .T. )

   IF hProcess != -1

      nState := hb_ProcessValue( hProcess, .T. )

      WHILE nState <> -1

         nBytes := FRead( hStdOut, @cBuff, 1024 /* cBuff length */ )

         IF nBytes == 0
            EXIT
         ENDIF

         nState := hb_ProcessValue( hProcess, .T. )

      END

      cBuff   := StrTran( cBuff, Chr( 13 ) )
      cBuff   := StrTran( cBuff, Chr( 10 ) )
      cResult := CharRem( " ", cBuff )

      hb_ProcessClose( hProcess )

   ENDIF

Return cResult

I hope to be proved helpful
Salu2
 --
Pierpaolo Martinello
IW1CUY Ham Radio From Biella Italy
Linux User 177880  

gvar...@gmail.com

unread,
Jul 26, 2021, 1:25:06 PM7/26/21
to Harbour Users
Hello Pierpaolo Martinello:
Thank you very much, it worked well.
BR
GVARONAS
Reply all
Reply to author
Forward
0 new messages