从 Excel VBA 通过 FTP 上传文件
需要从 Excel VBA 将文件 (file.txt) 上传到服务器 (ftp.server.com)。 (不一定是 FTP,只需要能够将文件放在那里并取回,并且我在 GoDaddy 共享托管上有一个服务器)
我尝试的是运行此脚本:
ftp -s:script.txt
:
open ftp.server.com
USER
PASS
lcd c:\
put file.txt
disconnect
bye
script.txt 我得到的错误是:
425 无法打开与端口 53637 的数据连接:连接超时
Google 告诉我需要进入被动模式,但命令行 ftp.exe
客户端不允许这样做。
我是否有更简单的 FTP 替代方案,或者是否有更好的方法通过 VBA 上传文件(无需命令行解决方法)?
我正在考虑使用 DROPBOX(但我真的不想在所有需要该程序的工作站上安装该程序)。
Need to upload a file (file.txt) to a server (ftp.server.com) from Excel VBA.
(does not have to be necessarily FTP, just need to be able to put the file there and get it back, and I've got a server on GoDaddy shared hosting)
What I tried was to run this script:
ftp -s:script.txt
script.txt:
open ftp.server.com
USER
PASS
lcd c:\
put file.txt
disconnect
bye
The error I get is:
425 Could not open data connection to port 53637: Connection timed out
Google tells me I need to go to passive mode, but the command-line ftp.exe
client doesn't allow that.
Do I have an easier alternative to FTP, or is there a better way to upload a file via VBA (without the command-line workaround)?
I'm thinking about using DROPBOX (but I really don't want to have to install this program on all the workstations that will need the program).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您无法使用 Windows
ftp.exe
(特别是因为它不支持被动模式和 TLS/SSL),您可以使用另一个命令行 FTP 客户端。例如,要使用 WinSCP 脚本 上传文件,请使用:
为了方便阅读,上面运行了这些WinSCP 命令:
您可以将命令放入脚本文件并使用
/script=
命令行参数,类似于ftp -s:
,而不是/command
。请参阅将 Windows FTP 脚本转换为 WinSCP 脚本指南。
您甚至可以让 WinSCP GUI 为您生成 FTP 上传脚本。
WinSCP 默认为被动模式。
您还可以使用 FTPS (TLS/SSL):
或者您可以使用 来自 VBA 代码的通过 COM 的 WinSCP .NET 程序集。
(我是 WinSCP 的作者)
If you cannot use the Windows
ftp.exe
(particularly because it does not support the passive mode and TLS/SSL), you can use another command-line FTP client.For example to upload a file using WinSCP scripting, use:
To ease reading, the above runs these WinSCP commands:
You can put the commands to a script file and run the script with
/script=
command-line parameter, similarly to theftp -s:
, instead of the/command
.See the guide to Converting Windows FTP script to WinSCP script.
You can even have WinSCP GUI generate the FTP upload script for you.
WinSCP defaults to the passive mode.
You can also use FTPS (TLS/SSL):
Alternatively you can use WinSCP .NET assembly via COM from the VBA code.
(I'm the author of WinSCP)
迭戈,我已经成功使用下面的代码很多年了。该代码从主机获取文件,但我确信可以对其进行修改以将文件放在那里。
Diego, I've used the code below successfully for years. The code gets files from the host, but I'm sure it can be modified to put files there instead.
http://winscp.net 是免费的,可编写脚本,支持被动模式,绝对是优秀的。
http://winscp.net is free, scriptable, supports passive mode and is definitely EXCELLENT.
经过大量研究,我找到了一种将文件上传到 FTP 位置的方法,无需任何 .ocx 文件互联网控制文件。这对我有用......
请根据您的需要更改值
After lot of research I found a method to upload file to FTP location without any .ocx file internet control file. This worked for me....
Please change values as per your needs
我也无法使被动模式与命令提示符一起使用,但我发现资源管理器工作得更快、更高效。
我在另一个子例程中指定了可以更改的内容:
以及我找到并修改为我使用的代码:
I could not make the passive mode work with the command prompt either, but I found out explorer works faster and more efficiently.
I have specified in an other subroutine what could change:
And the code I found and modified to my use:
上面的脚本很棒,我使用以下命令上传文件并将输出记录到文件中,这在调试时很有用,而且 Windows ftp 无法执行被动模式是一个常见的误解,进入被动模式的命令是 ” quote pasv" (我已将其添加到脚本中
The above script is great I used the following commands to upload files as well as log the output to a file which is useful when debugging also it is a common misconception that windows ftp cannot do passive mode the command to go passive is "quote pasv" (I have added this to the script