Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Simple Drag and Drop FTP file uploader (single directory)


  • Please log in to reply
13 replies to this topic
icefreez
  • Members
  • 178 posts
  • Last active: Jul 25 2014 02:52 PM
  • Joined: 15 May 2007
There have been a few flavors of uploaders. I found Trubbleguy's for updating files, but I was looking for something very simple. I just wanted to drop files in a GUI and get a URL to paste.

Usage:
For example I am chatting, "Ohh you should see the funny picture of Tim last weekend. One second let me upload it." I want a link to my image fast. All I want to do is drag a file and get a URL to it quick.
This script does just that. Just drag and drop a file into the window (after setting up the settings for your FTP server) and you will get URL's for your files. You can have it launch the files so you can copy the links to the clipboard (only the last one will be saved).

Special thanks goes to olfen for his FTP functions.

Posted Image

Code:
;You can hardcode your settings in here so you don't have to type them all the time.
FTPURL = 									;FTP URL.
FTPU = 										;FTP User Name
FTPP = 										;FTP Password
PORT = 										;FTP Port
remotelocation = 							;Remote folder to drop files into.
weburl=http://								;URL of the folder these files will be accessable from.

;If you don't want a GUI to always change your info just remove this section.
gui, add, text,,FTP URL
gui, add, edit,w400 h20,%FTPURL%
gui, add, text,,FTP User Name
gui, add, edit,w400 h20,%FTPU%
gui, add, text,,FTP Password
gui, add, edit,w400 h20,%FTPP%
gui, add, text,,Port
gui, add, edit,w40 h20,%PORT%
gui, add, text,,Remote folder to drop files into.
gui, add, edit,w400 h20,%remotelocation%
gui, add, text,,URL of the folder these files will be accessable from.
gui, add, edit,w400 h20,%weburl%

;Create GUI
gui, add, text,, Copy last to clipboard.
gui, add, checkbox, vclipboardcheck Checked
gui, add, text,, Open URL after upload.
gui, add, checkbox, vrunafter Checked

gui, add, text, y+20 w400, Drop files to upload here.
gui, add, edit, readonly w400 h80 vstatus
gui, show,,Simple Folder FTP
return

;Process a file once you drop a file in.
GuiDropFiles:
gui, submit, nohide
Loop, parse, A_GuiControlEvent, `n
{
	;get file name without path.
	SplitPath, A_LoopField, name
	
	;Connect and upload file.
	FtpOpen(FTPURL, PORT, FTPU , FTPP)
	FtpSetCurrentDirectory(remotelocation)
	FtpPutFile(A_LoopField, name)
	FtpClose()
	
	;update status to show file uploaded
	updatemsg(weburl name)
	
	;Copy to Clipboard and Run Options
	if(runafter=1)
		run, %weburl%%name%
	if(clipboardcheck=1)
		clipboard=%weburl%%name%
}
Return

GuiClose:
GuiEscape:
ExitApp

updatemsg(msg)
{
	gui, submit, nohide
	global status
	if (status <> " ")
		updatestatus = %msg%`n%Status%
	GuiControl, ,Status, %updatestatus%
}

;------------------------------------FTP Functions begin.------------------------------------
;FTP functions coded by olfen (http://www.autohotkey.com/forum/topic10393.html)

/*
http://msdn.microsoft.com/library/en-us/wininet/wininet/ftp_sessions.asp
http://msdn.microsoft.com/library/en-us/wininet/wininet/internetopen.asp
http://msdn.microsoft.com/library/en-us/wininet/wininet/internetconnect.asp
*/

FtpCreateDirectory(DirName) {
global ic_hInternet
r := DllCall("wininet\FtpCreateDirectoryA", "uint", ic_hInternet, "str", DirName)
If (ErrorLevel != 0 or r = 0)
return 0
else
return 1
}

FtpRemoveDirectory(DirName) {
global ic_hInternet
r := DllCall("wininet\FtpRemoveDirectoryA", "uint", ic_hInternet, "str", DirName)
If (ErrorLevel != 0 or r = 0)
return 0
else
return 1
}

FtpSetCurrentDirectory(DirName) {
global ic_hInternet
r := DllCall("wininet\FtpSetCurrentDirectoryA", "uint", ic_hInternet, "str", DirName)
If (ErrorLevel != 0 or r = 0)
return 0
else
return 1
}

FtpPutFile(LocalFile, NewRemoteFile="", Flags=0) {
;Flags:
;FTP_TRANSFER_TYPE_UNKNOWN = 0 (Defaults to FTP_TRANSFER_TYPE_BINARY)
;FTP_TRANSFER_TYPE_ASCII = 1
;FTP_TRANSFER_TYPE_BINARY = 2
If NewRemoteFile=
NewRemoteFile := LocalFile
global ic_hInternet
r := DllCall("wininet\FtpPutFileA"
, "uint", ic_hInternet
, "str", LocalFile
, "str", NewRemoteFile
, "uint", Flags
, "uint", 0) ;dwContext
If (ErrorLevel != 0 or r = 0)
return 0
else
return 1
}

FtpGetFile(RemoteFile, NewFile="", Flags=0) {
;Flags:
;FTP_TRANSFER_TYPE_UNKNOWN = 0 (Defaults to FTP_TRANSFER_TYPE_BINARY)
;FTP_TRANSFER_TYPE_ASCII = 1
;FTP_TRANSFER_TYPE_BINARY = 2
If NewFile=
NewFile := RemoteFile
global ic_hInternet
r := DllCall("wininet\FtpGetFileA"
, "uint", ic_hInternet
, "str", RemoteFile
, "str", NewFile
, "int", 1 ;do not overwrite existing files
, "uint", 0 ;dwFlagsAndAttributes
, "uint", Flags
, "uint", 0) ;dwContext
If (ErrorLevel != 0 or r = 0)
return 0
else
return 1
}

FtpGetFileSize(FileName, Flags=0) {
;Flags:
;FTP_TRANSFER_TYPE_UNKNOWN = 0 (Defaults to FTP_TRANSFER_TYPE_BINARY)
;FTP_TRANSFER_TYPE_ASCII = 1
;FTP_TRANSFER_TYPE_BINARY = 2
global ic_hInternet
fof_hInternet := DllCall("wininet\FtpOpenFileA"
, "uint", ic_hInternet
, "str", FileName
, "uint", 0x80000000 ;dwAccess: GENERIC_READ
, "uint", Flags
, "uint", 0) ;dwContext
If (ErrorLevel != 0 or fof_hInternet = 0)
return -1

FileSize := DllCall("wininet\FtpGetFileSize", "uint", fof_hInternet, "uint", 0)
DllCall("wininet\InternetCloseHandle",  "UInt", fof_hInternet)
return, FileSize
}


FtpDeleteFile(FileName) {
global ic_hInternet
r :=  DllCall("wininet\FtpDeleteFileA", "uint", ic_hInternet, "str", FileName)
If (ErrorLevel != 0 or r = 0)
return 0
else
return 1
}

FtpRenameFile(Existing, New) {
global ic_hInternet
r := DllCall("wininet\FtpRenameFileA", "uint", ic_hInternet, "str", Existing, "str", New)
If (ErrorLevel != 0 or r = 0)
return 0
else
return 1
}

FtpOpen(Server, Port=21, Username=0, Password=0 ,Proxy="", ProxyBypass="") {
IfEqual, Username, 0, SetEnv, Username, anonymous
IfEqual, Password, 0, SetEnv, Password, anonymous

If (Proxy != "")
AccessType=3
Else
AccessType=1
;#define INTERNET_OPEN_TYPE_PRECONFIG                    0   // use registry configuration
;#define INTERNET_OPEN_TYPE_DIRECT                       1   // direct to net
;#define INTERNET_OPEN_TYPE_PROXY                        3   // via named proxy
;#define INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY  4   // prevent using java/script/INS

global ic_hInternet, io_hInternet, hModule
hModule := DllCall("LoadLibrary", "str", "wininet.dll")

io_hInternet := DllCall("wininet\InternetOpenA"
, "str", A_ScriptName ;lpszAgent
, "UInt", AccessType
, "str", Proxy
, "str", ProxyBypass
, "UInt", 0) ;dwFlags

If (ErrorLevel != 0 or io_hInternet = 0) {
FtpClose()
return 0
}

ic_hInternet := DllCall("wininet\InternetConnectA"
, "uint", io_hInternet
, "str", Server
, "uint", Port
, "str", Username
, "str", Password
, "uint" , 1 ;dwService (INTERNET_SERVICE_FTP = 1)
, "uint", 0 ;dwFlags
, "uint", 0) ;dwContext

If (ErrorLevel != 0 or ic_hInternet = 0)
return 0
else
return 1
}

FtpClose() {
global ic_hInternet, io_hInternet, hModule
DllCall("wininet\InternetCloseHandle",  "UInt", ic_hInternet)
DllCall("wininet\InternetCloseHandle",  "UInt", io_hInternet)
DllCall("FreeLibrary", "UInt", hModule)
}


  • Guests
  • Last active:
  • Joined: --
can you please tell me whether i can use it on
photobucket/picasa/flicker / pict.com etc website?

  • Guests
  • Last active:
  • Joined: --
Does it support passive mode ftp transters?

icefreez
  • Members
  • 178 posts
  • Last active: Jul 25 2014 02:52 PM
  • Joined: 15 May 2007
It looks like photobucket supports ftp uploads so yes you can upload to photobucket. Just use the login info from this page.

I am not sure if Olfen's com FTP functions support passive mode. There is some referance to a mod of his code that looks like it supports using passive mode here.

ribbet.1
  • Members
  • 198 posts
  • Last active: Feb 07 2012 01:21 AM
  • Joined: 20 Feb 2007
This looks really sweet. I'm going to have to try this one. Does it overwrite by default, or no? Can I make it do that if I want?

Cheers, ribbet.1

icefreez
  • Members
  • 178 posts
  • Last active: Jul 25 2014 02:52 PM
  • Joined: 15 May 2007
Yes it overwrites by default.

ribbet.1
  • Members
  • 198 posts
  • Last active: Feb 07 2012 01:21 AM
  • Joined: 20 Feb 2007
Sweet!

Murp-e
  • Members
  • 531 posts
  • Last active: Sep 27 2011 11:44 AM
  • Joined: 12 Jan 2007
icefreez: Thank you for sharing, I've been wanting to make a similar program for a long time, for the same reasons as you. Today, I stumbled across a function called ListViewAdvanced which -among other things- lets you insert progress bars inside the cells of a standard listview control. If an amateur programmer were to combine that functionality with libcurl he might have ended up with something like this cruft (a new word I learned while looking up foobar):

#SingleInstance, force 
#NoEnv 
#include libcurl.ahk ; http://www.autohotkey.com/forum/topic32019.html 
#Include LVA.ahk

hLVIL := IL_Create(2) 
IL_Add(hLVIL, "shell32.dll", 1) 
IL_Add(hLVIL, "shell32.dll", 2) 
Gui, Add, ListView, w450 h340 vTLV gLVClick AltSubmit +LV0x2 grid, File|Progress
LV_SetImageList(hLVIL, 1) 
LV_ModifyCol(1, 300) 
LV_ModifyCol(2, 100) 
Gui, Show,, Upload-her
LVA_ListViewAdd("TLV", "AR ac cbsilver") 
;LVA_SetProgressBar("TLV", 1, 2, "s0xD8CB27 e0xFF91FF r200") 
;LVA_SetProgressBar("TLV", 1, 2, "") 
;LVA_SetProgressBar("TLV", 2, 2, "") 
;LVA_SetProgressBar("TLV", 3, 2, "") 
OnMessage("0x4E", "LVA_OnNotify") 

if ( CurlGlobalInit( "libcurl" ) != 0 ) 
   ExitApp 
hCurlEasy := CurlEasyInit() 
CurlEasyDefineOptions() 
CurlGetInfoDefine() 

CurlShowErrors() 
return


startupload:
count := LV_GetCount()
loop, %count%
{
 UploadComplete = 0
 RowNum := A_Index
 LV_GetText(FilePath, RowNum)
 SplitPath, FilePath, OutFileName
 LVA_SetProgressBar("TLV", RowNum, 2, "")

Url = ftp://USERNAME:PASSWORD@ftp.example.com/mydir/%OutFileName%
hFile := OpenFileForRead(FilePath) 

CurlEasySetOption( hCurlEasy, CURLOPT_URL, &URL ) 
CurlEasySetOption( hCurlEasy, CURLOPT_UPLOAD, 1) 
CurlEasySetOption( hCurlEasy, CURLOPT_READDATA, hFile) 

CurlEasySetOption( hCurlEasy, CURLOPT_NOPROGRESS, 0 ) 
pCurlProgressFunction := RegisterCallback("CurlProgressFunction", "C F") 
CurlEasySetOption( hCurlEasy, CURLOPT_PROGRESSFUNCTION, pCurlProgressFunction ) 

pCurlReadFunction := RegisterCallback("CurlReadFunction", "C F") 
CurlEasySetOption( hCurlEasy, CURLOPT_READFUNCTION, pCurlReadFunction ) 

If ( CurlEasyPerform( hCurlEasy ) ) 
  MsgBox % CURL_ERROR ;% 

  progress off 
CloseFile(hFile) 

 while UploadComplete = 0
 {
  sleep, 100
 }
}
CurlEasyCleanup( hCurlEasy ) 
CurlFreeLibrary()
return 


CurlProgressFunction(clientp, dltotal_l, dltotal_h, dlnow_l, dlnow_h, ultotal_l, ultotal_h, ulnow_l, ulnow_h) 
{ 
   global hFile 
   global RowNum
   global UploadComplete
   ;ultotal := MergeDouble( ultotal_l, ultotal_h ) 
   ultotal := GetFileSize(hFile) ;This shouldn't be necessary, but it was a quick fix. 
   ulnow := MergeDouble( ulnow_l, ulnow_h ) 
    
   ;TrayTip, Upload-her, "%ulnow%" of "%ultotal%" 
   KBTotal := Round( ultotal / 1024, 2 ) 
   KBNow := Round( ulnow / 1024, 2 ) 
   Percent := Round( ulnow / ultotal * 100, 2 ) 
   if Percent = 100
    UploadComplete = 1
   else
    UploadComplete = 0
   ;Progress, %Percent%, %KBNow% of %KBTotal% KB (%Percent% `%) 
   LVA_Progress("TLV",RowNum,2,Percent)

   Return 0 
} 






CurlReadFunction(pBuffer, size, nitems, pOutStream) 
{ 
   global hFile 
   return ReadFile(hFile, pBuffer, size*nitems) 
} 


;Sean http://www.autohotkey.com/forum/viewtopic.php?t=19608 
ReadFile(hFile, pBuffer, nSize = 1024) 
{ 
   DllCall("ReadFile", "Uint", hFile, "Uint", pBuffer, "Uint", nSize, "UintP", nSize, "Uint", 0) 
   Return nSize 
} 

;Sean http://www.autohotkey.com/forum/viewtopic.php?t=19608 
GetFileSize(hFile) 
{ 
   DllCall("GetFileSizeEx", "Uint", hFile, "int64P", nSize) 
   Return nSize 
} 


;PhiLho http://www.autohotkey.com/forum/viewtopic.php?t=7549 
/* 
// Open the file for reading. 
// Return the file handle to provide in further read operations and in the final close operation, 
// or INVALID_HANDLE_VALUE if an error was found. 
*/ 
OpenFileForRead(_filename) 
{ 
   local handle 

   handle := DllCall("CreateFile" 
         , "Str", _filename      ; lpFileName 
         , "UInt", 0x80000000      ; dwDesiredAccess (GENERIC_READ) 
         , "UInt", 3      ; dwShareMode (FILE_SHARE_READ|FILE_SHARE_WRITE) 
         , "UInt", 0      ; lpSecurityAttributes 
         , "UInt", 3      ; dwCreationDisposition (OPEN_EXISTING) 
         , "UInt", 0      ; dwFlagsAndAttributes 
         , "UInt", 0)   ; hTemplateFile 
   If (handle = INVALID_HANDLE_VALUE or handle = 0) 
   { 
      ErrorLevel = -1 
   } 
   If (ErrorLevel != 0) 
      Return INVALID_HANDLE_VALUE   ; Couldn't open the file 
   Return handle 
} 

;PhiLho http://www.autohotkey.com/forum/viewtopic.php?t=7549 
/* 
// Close the file. 
*/ 
CloseFile(_handle) 
{ 
   local result 

   result := DllCall("CloseHandle", "UInt", _handle) 
   If (result = 0) 
   { 
      ErrorLevel = -1 
   } 
}






;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

GuiDropFiles:
FileList = %A_GuiEvent%
Sort, FileList
count := 0

Loop, parse, FileList, `n
{
 ;SplitPath, A_LoopField, OutFileName
 ;LV_Insert(1,"", OutFileName)
 LV_Insert(1,"", A_LoopField)
 count := count + 1
}
gosub, startupload
return


GoBut: 
Loop, 100 
{ 
  LVA_Progress("TLV",1,2,A_Index) 
  Sleep, 100 
} 
return 

LVClick: 
  if (A_GuiEvent = "Normal") 
  { 
    LVA_GetCellNum(0, A_GuiControl) 
    GuiControl,,Label_Row, % LVA_GetCellNum("Row") 
    GuiControl,,Label_Col, % LVA_GetCellNum("Col") 
  } 
return 

GuiClose: 
ExitApp 
return
The above script is a simple gui with one listview. When you drop files on it, the files will begin to upload one at a time and the progress of the file currently being uploaded is displayed in the cells of the listview. This is very similar to how FileZilla's gui looks when uploading files. To test it you need to edit this line to and change the username, password, ftp.example.com and mydir with your own data:
Url = ftp://USERNAME:PASSWORD@ftp.example.com/mydir/%OutFileName%
Note that although the script will correctly upload the first batch of files dropped on the gui, it gets all messed up afterwards because the code is extremely sloppy. I just wanted to see if I could combine ListViewAdvanced with libcurl and thought this would be as good a thread as any to post the results.

[Edit] You also need to include LVA.ahk, libcurl.ahk and libcurl.dll needs to be in your scriptdir

The same functionality could be used for downloads. So basically, it's entirely feasible to recreate the standard functionality of an FTP manager in AutoHotkey.

I'd like to create a script similar to yours, but compile it with a password (So that the FTP password isn't stored anywhere) and fileinstall libcurl.dll so that it's a single portable executable.

SoggyDog
  • Members
  • 803 posts
  • Last active: Mar 04 2013 06:27 AM
  • Joined: 02 May 2006
LVA.ahk --> <!-- m -->http://www.autohotke...pic.php?t=43242<!-- m -->
libcurl.ahk --> <!-- m -->http://www.autohotke...pic.php?t=32019<!-- m -->
libcurl.dll --> <!-- m -->http://curl.haxx.se/libcurl/<!-- m -->

eakinasila
  • Members
  • 1 posts
  • Last active: Nov 25 2009 07:15 AM
  • Joined: 21 Nov 2009
How can i create my own ftp server and swap files with people ? Is this the same as peer-to-peer. How can i create my own ftp server and swap files with people. I have two computers I would like to experiment and send files. Is there a program called evil FTP?

Murp-e
  • Members
  • 531 posts
  • Last active: Sep 27 2011 11:44 AM
  • Joined: 12 Jan 2007
eaknasila: Yes, there are many programs which you can use to create your own FTP server. FileZilla has a client and a server program:
<!-- m -->http://filezilla-project.org<!-- m -->. Install and run the server on one computer and the client on the other. I'm no expert on this, but if you run into problems you may have to specifically open certain ports in your firewalls. Good luck.

Google says Evil FTP is a trojan virus: <!-- m -->http://www.dark-e.co... ... ndex.shtml<!-- m -->

N.B. I just realized this is completely off topic, if you want to continue discussing it I suggest you start a new thread in General Chat.

Bizibill
  • Guests
  • Last active:
  • Joined: --
Hi icefreez,
I like your Simple Drag and Drop FTP file uploader (single directory). It is exactly what I am looking for and maybe a little more. I just can't seem to get it to work. I believe that it is logging into my FTP site ok but when I drag/drop a file it gets the http website (for viewing) added as the path to the file. Naturally, since there is no such file in that path, nothing gets uploaded.

I tried putting C:\ in the URL space and it does find the file there and puts it on the clipboard fine but again doesn't upload. I tried looking at the code and have some understanding of the upload part but I'm not familiar with the way you get the file to upload part.

What I am really looking to do is when I run this .ahk program, is to upload a .txt file onto my FTP site and have it land in a folder there. That's it. Any help would be appreciated.
Thanks
Bizibill

FileUp
  • Guests
  • Last active:
  • Joined: --
Hi,

You can upload to FTP in browser with an applet such as JFileUpload.
See: http://www.jfileuplo... ... index.html

It supports regular FTP, FTPS (explicit and implicit) and SFTP (FTP + SSH). It can resume broken transfer too.

Cheers.

fragman
  • Members
  • 1591 posts
  • Last active: Nov 12 2012 08:51 PM
  • Joined: 13 Oct 2009
The script I'm currently working on can upload selected files from explorer by pressing CTRL+U, as well as take screenshots of screen/window or use image from clipboard and upload that.
I hope to be able to publish it sometime soon.