Author Topic: Download files  (Read 5459 times)

0 Members and 1 Guest are viewing this topic.

Offline Juanjogomez

  • Forum Regular
  • Posts: 117
    • View Profile
Download files
« on: December 25, 2021, 01:48:44 am »
Hello everyone and Merry Christmas.

I have tried continuing this thread, but I think it is closed. https://qb64forum.alephc.xyz/index.php?topic=4411.msg138383#msg138383

I keep trying how to download files and with the examples that you gave me in the other thread, I still can't get it.


I'm trying to download a file from my dropbox with this code, but nothing is downloading.

Code: QB64: [Select]
  1. DEFINT A-Z
  2.  
  3. CrLf$ = CHR$(13) + CHR$(10) ' carriage return + line feed ASCII characters
  4.  
  5. Host = _OPENHOST("TCP/IP:319")
  6. IF Host THEN
  7.     PRINT "> Server started succesfully."
  8.  
  9.     '// Change this to the file's public link
  10.     IP_File$ = "www.dropbox.com/s/t6y91q4yjys35j7/ALTERAN.exe?dl=0" 'a Drop Box link
  11.  
  12.     URL$ = LEFT$(IP_File$, INSTR(IP_File$, "/") - 1)
  13.     Path$ = MID$(IP_File$, INSTR(IP_File$, "/"))
  14.     Client& = _OPENCLIENT("TCP/IP:80:" + URL$)
  15.     IF Client& THEN
  16.         Request$ = "GET " + Path$ + " HTTP/1.1" + CrLf$ + "Host:" + URL$ + CrLf$ + CrLf$
  17.         PRINT Request$
  18.         PUT #Client&, , Request$
  19.         DO: _LIMIT 20 '              load response header
  20.             GET #Client&, , Dat$
  21.             Header$ = Header$ + Dat$
  22.         LOOP UNTIL INSTR(Header$, CrLf$ + CrLf$) ' Loop until 2 CRLFs (end of HTML header) are found
  23.         PRINT "Header Done."
  24.         PRINT Header$
  25.  
  26.         ' Get file size from header
  27.         SizePos = INSTR(UCASE$(Header$), "CONTENT-LENGTH:") + 16
  28.         SizeEnd = INSTR(SizePos, Header$, CrLf$)
  29.         FileSize& = VAL(MID$(Header$, SizePos, (SizeEnd - SizePos) + 1))
  30.         PRINT "File size is"; FileSize&; "bytes"
  31.         EndPos = INSTR(Header$, CrLf$ + CrLf$) + 4
  32.         Response$ = MID$(Header$, EndPos) ' get data after header already downloaded
  33.  
  34.         start = 1 '// Get file name from original URL path if necessary
  35.         DO '// Change this to destination local file name and path...
  36.             posit = INSTR(start, IP_File$, "/")
  37.             IF posit THEN lastpos = posit: start = posit + 1
  38.         LOOP UNTIL posit = 0
  39.         File$ = MID$(IP_File$, lastpos + 1) 'beware of tag suffixes
  40.         OPEN File$ FOR BINARY AS #1
  41.         DO: _LIMIT 20
  42.             PUT #1, , Response$
  43.             GET #Client&, , Response$
  44.         LOOP UNTIL LOF(1) >= FileSize&
  45.         PRINT "File download completed!"
  46.         CLOSE #1
  47.     ELSE
  48.         PRINT "Failed to connect."
  49.     END IF
  50.     PRINT "Failed to create server connection..."

Could someone pass me some code to download that file or another, just renaming?

Thanks in advance.



Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Download files
« Reply #1 on: December 25, 2021, 02:48:19 am »
This code works for HTTP and HTTPS files. Windows only.

Code: QB64: [Select]
  1.  
  2. Const INTERNET_OPEN_TYPE_DIRECT = 1
  3.  
  4. Const INTERNET_DEFAULT_HTTP_PORT = 80
  5. Const INTERNET_DEFAULT_HTTPS_PORT = 443
  6.  
  7. Const INTERNET_SERVICE_HTTP = 3
  8.  
  9. 'Flags
  10. Const INTERNET_FLAG_SECURE = &H00800000
  11. Const INTERNET_FLAG_RELOAD = &H80000000
  12.  
  13. Const HTTP_QUERY_CONTENT_LENGTH = 5
  14.  
  15. Const TRUE = 1
  16. 'CONST FALSE = 0
  17.  
  18.     Function InternetOpen%& Alias "InternetOpenA" (ByVal lpszAgent As _Offset, Byval dwAccessType As Long, Byval lpszProxy As _Offset, Byval lpszProxyBypass As _Offset, Byval dwFlags As Long)
  19.     Function InternetConnect%& Alias "InternetConnectA" (ByVal hInternet As _Offset, Byval lpszServerName As _Offset, Byval nServerPort As Integer, Byval lpszUserName As _Offset, Byval lpszPassword As _Offset, Byval dwService As Long, Byval dwFlags As Long, Byval dwContext As _Offset)
  20.     Function HTTPOpenRequest%& Alias "HttpOpenRequestA" (ByVal hConnect As _Offset, Byval lpszVerb As _Offset, Byval lpszObjectName As _Offset, Byval lpszVersion As _Offset, Byval lpszReferrer As _Offset, Byval lpszAcceptTypes As _Offset, Byval dwFlags As Long, Byval dwContext As _Offset)
  21.     Function HTTPSendRequest%% Alias "HttpSendRequestA" (ByVal hRequest As _Offset, Byval lpszHeaders As _Offset, Byval dwHeadersLength As Long, Byval lpOptional As _Offset, Byval dwOptionalLength As Long)
  22.     Sub InternetCloseHandle (ByVal hInternet As _Offset)
  23.     Function InternetReadFile%% (ByVal hFile As _Offset, Byval lpBuffer As _Offset, Byval dwNumberOfBytesToRead As Long, Byval lpdwNumberOfBytesRead As _Offset)
  24.     Function HTTPQueryInfo%% Alias "HttpQueryInfoA" (ByVal hRequest As _Offset, Byval dwInfoLevel As Long, Byval lpBuffer As _Offset, Byval lpdwBufferLength As _Offset, Byval lpdwIndex As _Offset)
  25.  
  26.     Function GetLastError& ()
  27.     Sub SetLastError (ByVal dwErrCode As Long)
  28.     Function FormatMessage& Alias "FormatMessageA" (ByVal dwFlags As Long, Byval lpSource As Long, Byval dwMessageId As Long, Byval dwLanguageId As Long, Byval lpBuffer As _Offset, Byval nSize As Long, Byval Arguments As _Offset)
  29.  
  30.     Function MAKELANGID& (ByVal p As Long, Byval s As Long)
  31.  
  32. Screen _NewImage(480, 80, 32)
  33.  
  34. _Title "URL Downloader"
  35. _ConsoleTitle "Enter Link"
  36. Dim filename As String
  37.  
  38.     Cls
  39.     Line Input "Link: ", link
  40.     Line Input "File Name : ", filename
  41. Loop Until link <> "" And filename <> ""
  42. _Title _Title$ + " - " + Mid$(filename, _InStrRev(filename, "\") + 1)
  43.  
  44.  
  45. DownloadLink link, filename
  46.  
  47. Sub DownloadLink (URL As String, File As String)
  48.     Dim As String URLFile
  49.     URLFile = URL
  50.     Dim As _Offset hsession
  51.     hsession = InternetOpen(0, INTERNET_OPEN_TYPE_DIRECT, 0, 0, 0)
  52.     If hsession = 0 Then
  53.         Cls
  54.         Print "Error : InternetOpen", ErrorMessage(GetLastError)
  55.         InternetCloseHandle hsession
  56.         Exit Sub
  57.     End If
  58.  
  59.     Dim As _Offset httpsession
  60.     URL = Mid$(URL, InStr(URL, "/") + 2)
  61.     URL = Mid$(URL, 1, InStr(URL, "/") - 1)
  62.     Dim As String intURL: intURL = URL + Chr$(0)
  63.     httpsession = InternetConnect(hsession, _Offset(intURL), INTERNET_DEFAULT_HTTPS_PORT, 0, 0, INTERNET_SERVICE_HTTP, 0, 0)
  64.     If httpsession = 0 Then
  65.         Cls
  66.         Print "Error : Internet Connect", ErrorMessage(GetLastError)
  67.         InternetCloseHandle hsession
  68.         Exit Sub
  69.     End If
  70.  
  71.     Dim As _Offset httpRequest
  72.     Dim As String sessiontype, location, accepttypes
  73.     sessiontype = "GET" + Chr$(0)
  74.     location = Mid$(URLFile, InStr(URLFile, URL) + Len(URL)) + Chr$(0)
  75.     accepttypes = "*/*" + Chr$(0)
  76.     httpRequest = HTTPOpenRequest(httpsession, _Offset(sessiontype), _Offset(location), 0, 0, _Offset(accepttypes), INTERNET_FLAG_RELOAD Or INTERNET_FLAG_SECURE, 0)
  77.     If httpRequest = 0 Then
  78.         Cls
  79.         Print "Error : HTTPOpenRequest", ErrorMessage(GetLastError)
  80.         InternetCloseHandle hsession
  81.         Exit Sub
  82.     End If
  83.  
  84.     Dim As Long sendrequest
  85.     Dim As String headers
  86.     headers = ""
  87.     sendrequest = HTTPSendRequest(httpRequest, 0, 0, 0, 0)
  88.     If sendrequest <> TRUE Then
  89.         Cls
  90.         Print "Error : HTTPSendRequest", ErrorMessage(GetLastError)
  91.         InternetCloseHandle hsession
  92.         Exit Sub
  93.     End If
  94.  
  95.  
  96.     Dim As _Byte query
  97.     Dim As String queryinfo
  98.     queryinfo = Space$(1024)
  99.     Dim As Long querylen
  100.     querylen = Len(queryinfo) - 1
  101.  
  102.     query = HTTPQueryInfo(httpRequest, HTTP_QUERY_CONTENT_LENGTH, _Offset(queryinfo), _Offset(querylen), 0)
  103.     If query <> TRUE Then
  104.         Cls
  105.         Print "Error : HTTPQueryInfo", ErrorMessage(GetLastError)
  106.         InternetCloseHandle hsession
  107.     End If
  108.  
  109.     Dim As _Unsigned _Integer64 bytesToRead
  110.     bytesToRead = Val(queryinfo)
  111.  
  112.     Dim As String szBuffer
  113.     szBuffer = Space$(4097)
  114.     Dim As _Unsigned _Integer64 dwRead, bytesRead
  115.     If _FileExists(File) Then
  116.         Kill File
  117.     End If
  118.     Open File For Binary As #1
  119.     Dim As _Byte a
  120.     Dim As String filedownload
  121.     Dim As Long errr, bytesForRate
  122.     Dim x!
  123.     Dim y!
  124.     Dim Rate!
  125.     Dim As Single ratetime
  126.     Do
  127.         x! = Timer
  128.         a = InternetReadFile(httpRequest, _Offset(szBuffer), Len(szBuffer) - 1, _Offset(dwRead))
  129.         errr = GetLastError
  130.         If dwRead > 0 Then
  131.             filedownload = Mid$(szBuffer, 1, dwRead)
  132.             Put #1, , filedownload
  133.             bytesRead = bytesRead + dwRead
  134.             bytesForRate = bytesForRate + dwRead
  135.             ratetime = timeElapsedSince(x!)
  136.             If _Round(ratetime) >= 1 Then
  137.                 Rate! = (bytesForRate / ratetime) / 1024
  138.                 bytesForRate = 0
  139.             End If
  140.             Cls
  141.             Print "Downloading to " + File
  142.             If bytesToRead <> 0 Then
  143.                 Select Case bytesRead
  144.                     Case Is < 1024
  145.                         Print Using "#### B downloaded of "; bytesRead;
  146.                     Case Is < (1024 ^ 2) And bytesRead >= 1024
  147.                         Print Using "####.## KB downloaded of "; (bytesRead / 1024);
  148.                     Case Is < (1024 ^ 3) And bytesRead >= (1024 ^ 2)
  149.                         Print Using "####.## MB downloaded of "; (bytesRead / (1024 ^ 2));
  150.                     Case Is < (1024 ^ 4) And bytesRead >= (1024 ^ 3)
  151.                         Print Using "####.## GB downloaded of "; (bytesRead / (1024 ^ 3));
  152.                 End Select
  153.                 Select Case bytesToRead
  154.                     Case Is < 1024
  155.                         Print Using "#### B"; bytesToRead
  156.                     Case Is < (1024 ^ 2) And bytesToRead >= 1024
  157.                         Print Using "####.## KB"; (bytesToRead / 1024)
  158.                     Case Is < (1024 ^ 3) And bytesToRead >= (1024 ^ 2)
  159.                         Print Using "####.## MB"; (bytesToRead / (1024 ^ 2))
  160.                     Case Is < (1024 ^ 4) And bytesToRead >= (1024 ^ 3)
  161.                         Print Using "####.## GB"; (bytesToRead / (1024 ^ 3))
  162.                 End Select
  163.                 Print Using "###.##%"; bytesRead / bytesToRead * 100
  164.             Else
  165.                 Select Case bytesRead
  166.                     Case Is < 1024
  167.                         Print Using "   ####  B downloaded"; bytesRead
  168.                     Case Is < (1024 ^ 2) And bytesRead >= 1024
  169.                         Print Using "####.## KB downloaded"; (bytesRead / 1024)
  170.                     Case Is < (1024 ^ 3) And bytesRead >= (1024 ^ 2)
  171.                         Print Using "####.## MB downloaded"; (bytesRead / (1024 ^ 2))
  172.                     Case Is < (1024 ^ 4) And bytesRead >= (1024 ^ 3)
  173.                         Print Using "####.## GB downloaded"; (bytesRead / (1024 ^ 3))
  174.                 End Select
  175.             End If
  176.             Select Case Rate!
  177.                 Case Is < 1024
  178.                     Print Using "Rate: #### Bps"; Rate!
  179.                 Case Is < (1024 ^ 2) And Rate! >= 1024
  180.                     Print Using "Rate: ####.## KBps"; Rate! / 1024
  181.                 Case Is < (1024 ^ 3) And Rate! >= (1024 ^ 2)
  182.                     Print Using "Rate: ####.## MBps"; Rate! / (1024 ^ 2)
  183.                 Case Is < (1024 ^ 4) And Rate! >= (1024 ^ 3)
  184.                     Print Using "Rate: ####.## GBps"; Rate! / (1024 ^ 3)
  185.             End Select
  186.             'Print "Rate="; _Round(Rate!); "KBps"
  187.             _Display
  188.         End If
  189.     Loop Until bytesRead = bytesToRead Or errr <> 0
  190.     If errr Then
  191.         Print "Error downloading file:"; errr
  192.         Close #1
  193.         InternetCloseHandle hsession
  194.         Kill File
  195.         Exit Sub
  196.     Else
  197.     End If
  198.     Close #1
  199.     InternetCloseHandle hsession
  200.     Cls
  201.     Select Case bytesRead
  202.         Case Is < 1024
  203.             Print Using "#### B downloaded of "; bytesRead;
  204.         Case Is < (1024 ^ 2) And bytesRead >= 1024
  205.             Print Using "####.## KB downloaded of "; (bytesRead / 1024);
  206.         Case Is < (1024 ^ 3) And bytesRead >= (1024 ^ 2)
  207.             Print Using "####.## MB downloaded of "; (bytesRead / (1024 ^ 2));
  208.         Case Is < (1024 ^ 4) And bytesRead >= (1024 ^ 3)
  209.             Print Using "####.## GB downloaded of "; (bytesRead / (1024 ^ 3));
  210.     End Select
  211.     Select Case bytesToRead
  212.         Case Is < 1024
  213.             Print Using "#### B"; bytesToRead
  214.         Case Is < (1024 ^ 2) And bytesToRead >= 1024
  215.             Print Using "####.## KB"; (bytesToRead / 1024)
  216.         Case Is < (1024 ^ 3) And bytesToRead >= (1024 ^ 2)
  217.             Print Using "####.## MB"; (bytesToRead / (1024 ^ 2))
  218.         Case Is < (1024 ^ 4) And bytesToRead >= (1024 ^ 3)
  219.             Print Using "####.## GB"; (bytesToRead / (1024 ^ 3))
  220.     End Select
  221.     Print Using "###.##%"; bytesRead / bytesToRead * 100
  222.     Print "Downloaded to " + File
  223.  
  224. Function timeElapsedSince! (startTime!)
  225.     If startTime! > Timer Then startTime! = startTime! - 86400
  226.     timeElapsedSince! = Timer - startTime!
  227.  
  228. Function ErrorMessage$ (errCode As Long)
  229.     Const FORMAT_MESSAGE_ALLOCATE_BUFFER = &H00000100
  230.     Const FORMAT_MESSAGE_FROM_SYSTEM = &H00001000
  231.     Const FORMAT_MESSAGE_IGNORE_INSERTS = &H00000200
  232.  
  233.     Const LANG_NEUTRAL = &H00
  234.     Const SUBLANG_DEFAULT = &H01
  235.  
  236.     Dim As _Offset lpMsgBuf
  237.     Dim As Long msg
  238.  
  239. msg = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER Or _
  240.                     FORMAT_MESSAGE_FROM_SYSTEM Or _
  241.                     FORMAT_MESSAGE_IGNORE_INSERTS, _
  242.                    0, _
  243.                     errCode, _
  244.                     MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), _
  245.                     _Offset(lpMsgBuf), _
  246.                    0, 0)
  247.  
  248.  
  249.     ErrorMessage = pointerToString(lpMsgBuf)
  250.  
  251. Function pointerToString$ (pointer As _Offset)
  252.         Function strlen%& (ByVal ptr As _Unsigned _Offset)
  253.     End Declare
  254.     Dim As _Offset length: length = strlen(pointer)
  255.     If length Then
  256.         Dim As _MEM pString: pString = _Mem(pointer, length)
  257.         Dim As String ret: ret = Space$(length)
  258.         _MemGet pString, pString.OFFSET, ret
  259.         _MemFree pString
  260.     End If
  261.     pointerToString = ret
Shuwatch!

Offline Juanjogomez

  • Forum Regular
  • Posts: 117
    • View Profile
Re: Download files
« Reply #2 on: December 25, 2021, 04:25:32 am »
I just tried it and it only downloaded 58 Kb, when the original has 2,758 Kb, what could be the problem? Thanks

Offline Juanjogomez

  • Forum Regular
  • Posts: 117
    • View Profile
Re: Download files
« Reply #3 on: December 25, 2021, 04:40:57 am »
I have tried it again and I have seen that when the 0 is changed to a 1, the program downloads perfectly.
With this link it does not work:

https://www.dropbox.com/s/t6y91q4yjys35j7/ALTERAN.exe?dl=0

And changing the final figure, it downloads it perfectly:

https://www.dropbox.com/s/t6y91q4yjys35j7/ALTERAN.exe?dl=1

I do not know why

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Download files
« Reply #4 on: December 25, 2021, 06:22:39 am »
I have tried it again and I have seen that when the 0 is changed to a 1, the program downloads perfectly.
With this link it does not work:

https://www.dropbox.com/s/t6y91q4yjys35j7/ALTERAN.exe?dl=0

And changing the final figure, it downloads it perfectly:

https://www.dropbox.com/s/t6y91q4yjys35j7/ALTERAN.exe?dl=1

I do not know why

The end character with dropbox is a toggle for "Download instantly, or wait for human interaction?"

If dl = 0, you get a series of responses for pop-ups asking you "Are you sure you want to save this file?  Where would you like to save this file?  This file might be dangerous, are you certain? "

If dl=1, you simply download the file.



Two kinds of ‘raw’ file download
Dropbox supports two kinds of downloads. One just does what you’d expect. Click a link, and the file downloads. The other renders a file in the browser. This is like when you click an MP3 link and get a little Quicktime-style music player in the middle of the browser window, or when you right-click an image on any webpage and choose to Open in New Tab, whereupon the image is displayed, alone, in the browser.

Force a Dropbox link to render in the browser
To force a Dropbox link to render in the browser, take the link and change dl=0 to raw=1. That is, it serves the “raw” file to your browser. Dropbox pro users can even serve HTML files, so they can host a simple webpage on Dropbox. The example link would look like this:

https://www.dropbox.com/s/a1b2c3d4ef5gh6/example.docx?raw=1

Force a Dropbox link to download
To force a Dropbox link to download a file direct, take the link and change dl=0 to dl=1. These codes mean “download disabled” and “download enabled.”

https://www.dropbox.com/s/a1b2c3d4ef5gh6/example.docx?dl=1
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Juanjogomez

  • Forum Regular
  • Posts: 117
    • View Profile
Re: Download files
« Reply #5 on: December 25, 2021, 06:33:14 am »
Thanks to all

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Download files
« Reply #6 on: December 25, 2021, 11:43:34 am »
@Juanjogomez Merry Christmas! Glad it worked for you
Shuwatch!

Offline Juanjogomez

  • Forum Regular
  • Posts: 117
    • View Profile
Re: Download files
« Reply #7 on: December 26, 2021, 03:11:15 am »
@SpriggsySpriggs MERRY CHRISTMAS!! Thanks for your help

Offline Colonel_Panic

  • Newbie
  • Posts: 54
    • View Profile
Re: Download files
« Reply #8 on: December 26, 2021, 03:07:17 pm »
I am watching this with heightened interest... one of my "end game"
strategies, is to download "stuff" and have 2 or more applications "exchange bytes"