Author Topic: Bagaimana cara download file image/gambar dari webserver  (Read 346 times)

0 Members and 1 Guest are viewing this topic.

Offline hendi_am

  • Newbie
  • *
  • Posts: 40
Bagaimana cara download file image/gambar dari webserver
« on: April 02, 2017, 03:57:18 PM »
Halo master, adakah yang tahu coding untuk download file image/gambar dari webserver?

Saya sudah mencoba beberapa coding, tapi yang berhasil download cuma melalui akses ftp, cuma sayang filenya tidak bisa dibuka, seperti contoh berikut :

PROCEDURE DOWNLOAD
PARAMETERS lcHost, lcUser, lcPwd, lcRemoteFile, lcNewFile, lnXFerType     

*.................................................................................
*:   Usage: DO ftpget WITH ;
*:         'ftp.host', 'name', 'password', 'source.file', 'target.file'[, 1 | 2]
*:
*:  Where:  lcHost       = Host computer IP address or name
*:          lcUser       = user name - anonymous may be used
*:          lcPwd        = password
*:          lcRemoteFile = source file name
*:          lcNewFile    = target file name
*:          lnXFerType   = 1 (default) for ascii, 2 for binary
*.................................................................................

*...set up API calls
DECLARE INTEGER InternetOpen IN wininet;
   STRING sAgent, INTEGER lAccessType, STRING sProxyName,;
   STRING sProxyBypass, STRING  lFlags

DECLARE INTEGER InternetCloseHandle IN wininet INTEGER hInet

DECLARE INTEGER InternetConnect IN wininet.DLL;
   INTEGER hInternetSession,;
   STRING  lcHost,;
   INTEGER nServerPort,;
   STRING  lcUser,;
   STRING  lcPassword,;
   INTEGER lService,;
   INTEGER lFlags,;
   INTEGER lContext

DECLARE INTEGER FtpGetFile IN wininet;
   INTEGER hftpSession, ;
   STRING  lcRemoteFile,;
   STRING  lcNewFile, ;
   INTEGER fFailIfExists,;
   INTEGER dwFlagsAndAttributes,;
   INTEGER dwFlags, ;
   INTEGER dwContext

lcHost       = ALLTRIM(lcHost)
lcUser       = ALLTRIM(lcUser)
lcPwd        = ALLTRIM(lcPwd)
lcRemoteFile = ALLTRIM(lcRemoteFile)
lcNewFile    = ALLTRIM(lcNewFile)

sAgent = "vfp"

sProxyName = CHR(0)     &&... no proxy
sProxyBypass = CHR(0)   &&... nothing to bypass
lFlags = 0              &&... no flags used

*... initialize access to Inet functions
hOpen = InternetOpen (sAgent, 1,;
   sProxyName, sProxyBypass, lFlags)

IF hOpen = 0
   WAIT WINDOW  "Unable to get access to WinInet.Dll" TIMEOUT 2
   RETURN
ENDIF

*... The first '0' says use the default port, usually 21.
hftpSession = InternetConnect (hOpen, lcHost,;
   0, lcUser, lcPwd, 1, 0, 0)   &&... 1 = ftp protocol

IF hftpSession = 0
   *... close access to Inet functions and exit
   = InternetCloseHandle (hOpen)
   WAIT WINDOW "Unable to connect to " + lcHost + '.' TIMEOUT 2
   RETURN
ELSE
   WAIT WINDOW "Connected to " + lcHost + " as: [" + lcUser + "]"  TIMEOUT 1
ENDIF

*... 0 to automatically overwrite file
*... 1 to fail if file already exists
fFailIfExists  = 0 
dwContext      = 0  &&... used for callback

WAIT WINDOW 'Transferring ' + lcRemoteFile + ' to ' + lcNewFile + '...' NOWAIT
lnResult = FtpGetFile (hftpSession, lcRemoteFile, lcNewFile,;
   fFailIfExists, 128, lnXFerType,;
   dwContext)

*... 128 = #define FILE_ATTRIBUTE_NORMAL     0x00000080
*... See CreateFile for other attributes

* close handles
= InternetCloseHandle (hftpSession)
= InternetCloseHandle (hOpen)

IF lnResult = 1
   *... successful download, do what you want here
   WAIT WINDOW 'Completed.' TIMEOUT 1
   MODI FILE (lcNewFile)
ELSE
   WAIT WINDOW  "Unable to download selected file" TIMEOUT 2
ENDIF

RETURN
ENDPROC

Setelah didownload, filenya sedikit lebih besar dari file image aslinya, misalkan aslinya 1 mb tapi dengan code diatas ketika selesai didownload menjadi 1,1 mb dan tidak bisa dibuka, tapi kalo bukan file image, bisa dibuka normal. Mohon bantuannya , terima kasih.


Offline doditasik

  • Full Member
  • *
  • Posts: 74
Re: Bagaimana cara download file image/gambar dari webserver
« Reply #1 on: April 03, 2017, 09:59:19 AM »
saya pernah pake Chilkat ActiveX (ada versi 32/64), disana ada beberapa procedure, silahkan improvisasi saja !

Offline doditasik

  • Full Member
  • *
  • Posts: 74
Re: Bagaimana cara download file image/gambar dari webserver
« Reply #2 on: April 03, 2017, 10:01:07 AM »
LOCAL loFtp
LOCAL lnSuccess
LOCAL lcLocalFilename
LOCAL lcRemoteFilename

loFtp = CreateObject('Chilkat_9_5_0.Ftp2')
ftpEventHandler = CreateObject("ftpEvents")
EventHandler(loFtp,ftpEventHandler)

*  Any string unlocks the component for the 1st 30-days.
lnSuccess = loFtp.UnlockComponent("Anything for 30-day trial")
IF (lnSuccess <> 1) THEN
    ? loFtp.LastErrorText
    RELEASE loFtp
    CANCEL
ENDIF

loFtp.Hostname = "ftp.someFtpServer.com"
loFtp.Username = "myLogin"
loFtp.Password = "myPassword"

*  Connect and login to the FTP server.
lnSuccess = loFtp.ConnectOnly()
IF (lnSuccess <> 1) THEN
    ? loFtp.LastErrorText
    RELEASE loFtp
    CANCEL
ENDIF

lnSuccess = loFtp.LoginAfterConnectOnly()
IF (lnSuccess <> 1) THEN
    ? loFtp.LastErrorText
    RELEASE loFtp
    CANCEL
ENDIF

*  Change to the remote directory where the file is located.
*  This step is only necessary if the file is not in the home directory
*  of the FTP account.
lnSuccess = loFtp.ChangeRemoteDir("junk")
IF (lnSuccess <> 1) THEN
    ? loFtp.LastErrorText
    RELEASE loFtp
    CANCEL
ENDIF

lcLocalFilename = "c:/temp/hamlet.xml"
lcRemoteFilename = "hamlet.xml"

*  Ensure that we get PercentDone callbacks.
loFtp.AutoGetSizeForProgress = 1

*  Download the file.
lnSuccess = loFtp.GetFile(lcRemoteFilename,lcLocalFilename)
IF (lnSuccess <> 1) THEN
    ? loFtp.LastErrorText
    RELEASE loFtp
    CANCEL
ENDIF

lnSuccess = loFtp.Disconnect()

? "File Downloaded!"

RELEASE loFtp

DEFINE CLASS ftpEvents AS SESSION OLEPUBLIC
IMPLEMENTS _IChilkatFtp2Events IN "Chilkat_9_5_0.Ftp2"

PROCEDURE _IChilkatFtp2Events_AbortCheck(abort As Number)
    * Application code goes here...

ENDPROC

PROCEDURE _IChilkatFtp2Events_BeginDownloadFile(path As String, nSkip As Number)
    * Application code goes here...
ENDPROC

PROCEDURE _IChilkatFtp2Events_BeginUploadFile(path As String, nSkip As Number)
    * Application code goes here...
ENDPROC

PROCEDURE _IChilkatFtp2Events_BinaryData(data As Variant)
    * Application code goes here...

ENDPROC

PROCEDURE _IChilkatFtp2Events_DownloadRate(byteCount As Number, bytesPerSec As Number)
    * Application code goes here...
ENDPROC

PROCEDURE _IChilkatFtp2Events_EndDownloadFile(path As String, byteCount As Number)
    * Application code goes here...
ENDPROC

PROCEDURE _IChilkatFtp2Events_EndUploadFile(path As String, byteCount As Number)
    * Application code goes here...
ENDPROC

PROCEDURE _IChilkatFtp2Events_PercentDone(pctDone As Number, abort As Number)
    * Application code goes here...
    ? "Percent Done: " + STR(pctDone)
ENDPROC

PROCEDURE _IChilkatFtp2Events_ProgressInfo(name As String, value As String)
    * Application code goes here...
    ? name + ": " + value
ENDPROC

PROCEDURE _IChilkatFtp2Events_TaskCompleted(task As Generic)
    * Application code goes here...

ENDPROC

PROCEDURE _IChilkatFtp2Events_TextData(data As String)
    * Application code goes here...

ENDPROC

PROCEDURE _IChilkatFtp2Events_UploadRate(byteCount As Number, bytesPerSec As Number)
    * Application code goes here...
ENDPROC

PROCEDURE _IChilkatFtp2Events_VerifyDeleteDir(path As String, nSkip As Number)
    * Application code goes here...
ENDPROC

PROCEDURE _IChilkatFtp2Events_VerifyDeleteFile(path As String, nSkip As Number)
    * Application code goes here...
ENDPROC

PROCEDURE _IChilkatFtp2Events_VerifyDownloadDir(path As String, nSkip As Number)
    * Application code goes here...
ENDPROC

PROCEDURE _IChilkatFtp2Events_VerifyUploadDir(path As String, nSkip As Number)
    * Application code goes here...
ENDPROC


ENDDEFINE

Offline hendi_am

  • Newbie
  • *
  • Posts: 40
Re: Bagaimana cara download file image/gambar dari webserver
« Reply #3 on: April 06, 2017, 07:28:21 AM »
Hatur nuhun kang Dody, chilkat bukannya berbayar kang?

Tapi sudah ketemu sih kang, dengan coding sederhana, gak lewat FTP sih tapi akses langsung ke folder webservernya yg sudah dikasih akses 'read' saja.

Local loRequest, lcUrl, lcFilename

lcUrl = "www.contoh.com/upload/images/gambar.jpg"
lcFilename = "d:\images\gambar.jpg"

loRequest = Createobject('MsXml2.XmlHttp')
loRequest.Open("GET",lcUrl,.F.)
loRequest.Send()
StrToFile(loRequest.ResponseBody,lcFilename)
« Last Edit: April 06, 2017, 08:12:02 AM by hendi_am »

Offline doditasik

  • Full Member
  • *
  • Posts: 74
Re: Bagaimana cara download file image/gambar dari webserver
« Reply #4 on: April 06, 2017, 10:33:10 AM »
iya berbayar.., kan kita jual lagi yang penting ada margin...he..he

 :thumbsup:bagus..lah