如何以编程方式执行包含“文件”的 CGI 脚本表单 输入上传输入字段?

发布于 2024-07-16 09:20:08 字数 1106 浏览 1 评论 0 原文

我刚刚(用 Perl)编写了一个简单的 Web 服务,它显示一个包含单个“文件”类型上传字段和一个提交按钮的 Web 表单。 我制作了这个简单的网页,目的是让人类可以使用它,并且不同机器上的脚本也可以以编程方式执行它。 我已经完成并测试了人类部分,现在我刚刚意识到我不知道如何在第二台机器上构造一个 URL,该 URL 在第一台机器上成功执行此 cgi 脚本(并获取从第二台机器上传到第一台机器)。

我确实有一些构建包含表单字段的 URL 的经验,但之前从未使用过“文件”类型上传输入字段,所以我有点挣扎。 也许我用错了。

如果我的简单 Web 服务是一个名为“myscript.pl”的 cgi-bin 脚本,并且文件上传字段名为“uploadthis”,并且我想要以编程方式上传的第二台计算机上的文件位于“C:\myfile” .txt”,那么我不应该能够在第二台计算机上的脚本中构造一个“http://machine1/cgi-bin/myscript.pl?uploadthis=C:\myfile.txt" 并且该文件以编程方式上传,就像人类单击浏览一样手动在 Web 表单上单击按钮并选择该文件并单击“提交”按钮?

当我在第一台机器上打开 cgi 脚本表单并浏览到该文件并单击“提交”时,它手动工作正常。 该文件似乎可以通过网络浏览器上传。 但是,当我在浏览器中输入上述 URL 以手动测试文件上传 URL,然后将该 URL 编程到另一台计算机上的脚本中时,上传的文件是零长度文件。 它创建了一个文件,但它完全是空的。 如果我使用已知的错误文件名,例如“http://machine1/cgi-bin,结果相同/myscript.pl?uploadthis=C:\myfilethatdoesntexist.txt”。 我究竟做错了什么?

我试图在几台装有 Apache 2.2 和 ActivePerl 5.10.0 的 Windows XP PC 上完成此任务,表单方法是“post”。

I've just written (in Perl) a simple web service that displays a web form containing a single "file" type upload field and a single Submit button. I made this simple web page with the intent that a human could use it and also that a script on a different machine could also execute it programatically. I got the human part all done and tested and now I just realized that I have no idea how to construct a URL on the second machine that successfully executes this cgi script on the first machine (and gets a file uploaded from the second machine to the first machine).

I do have some experience constructing URLs that contain form fields, but have never used the "file" type upload input field before, so I'm struggling with it a bit. Maybe I'm using it wrong.

If my simple web service is a cgi-bin script called "myscript.pl" and if the file upload field is called "uploadthis" and if the file on the second machine that I want to upload programatically is located in "C:\myfile.txt", then shouldn't I be able to just construct a URL in a script on the second machine of the form "http://machine1/cgi-bin/myscript.pl?uploadthis=C:\myfile.txt" and that file gets uploaded programatically just as if a human had clicked on the browse button on the web form manually and chose that file and clicked on the Submit button?

It works fine manually when I bring up the cgi script form on the first machine and browse to the file and click Submit. The file seems to get uploaded by the web browser just fine. But when I type in the above URL into my browser to test out the file upload URL manually before I program that URL into a script on another machine, the file that gets uploaded is a zero length file. It creates a file but it's completely empty. Same results if I use a known bad filename like "http://machine1/cgi-bin/myscript.pl?uploadthis=C:\myfilethatdoesntexist.txt". What am I doing wrong?

I'm trying to accomplish this task on a couple of Windows XP PCs with Apache 2.2 and ActivePerl 5.10.0 and the form method is "post".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半枫 2024-07-23 09:20:09

首先,post 参数不会出现在 URL 的末尾。 此外,不可能从浏览器(甚至使用 JavaScript)完全自动发送文件上传,只是为了防止用户偷渡式上传。

如果您想从浏览器上传,则始终必须选择文件或在框中输入文件名,或手动单击提交按钮。 如果所有这些都是自动化的,则文件将不会被上传。

要以编程方式测试文件上传,您可以使用curl:

curl -F "uploadthis=@C:\myfile.txt" http://machine1/cgi-bin/myscript.pl

您当然也可以编写一个程序将文件作为多部分/表单数据发布。

First of all, post parameters do not end up at the end of the URL. In addition, it is not possible to send a file upload completely automated from a browser (not even with JavaScript), just to prevent users from drive-by uploads.

If you want to upload from a browser, you will always have to select the file or type the filename into the box, or click the submit button manually. If all of these is automated, the file will not be uploaded.

To test file uploads programmatically, you can use curl:

curl -F "uploadthis=@C:\myfile.txt" http://machine1/cgi-bin/myscript.pl

You can of course write a program that posts files as multipart/form-data as well.

静若繁花 2024-07-23 09:20:09

您应该使用诸如 LWP 和 HTTP::Request::Common 或 < a href="http://search.cpan.org/dist/WWW-Mechanize/" rel="nofollow noreferrer">WWW::Mechanize。 两者都有文件上传的示例。 WWW::Mechanize 更简单,但您应该仅从 ppm 安装它。 LWP 包含在 ActiveState 中。

You should use such modules as LWP and HTTP::Request::Common or WWW::Mechanize. Both has examples with file uploading. WWW::Mechanize is simpler, but you should install it from ppm only. LWP is included into ActiveState.

故人如初 2024-07-23 09:20:09

我完全错过了“multipart/form-data”这个短语。 当我用谷歌搜索这句话时,我找到了答案。

与下面的示例类似的是我如何通过 perl 脚本解决我的问题:

use HTTP::Request::Common;
use LWP::UserAgent;

$ua = LWP::UserAgent->new;

my $response = $ua->request(
    POST 'http://machine1/cgi-bin/myscript.pl',
    Content_Type => 'form-data',
    Content      => [ 
                        uploadthis => ["C:\myfile.txt"],
                    ]
    );

if ($response->is_success) {
    print "Success.";
}
else {
    print "Failure.";
}

The phrase "multipart/form-data" was something that I had missed completely. When I Google'd that phrase, I found my answer.

Something similar to this example below is how I solved my problem from a perl script:

use HTTP::Request::Common;
use LWP::UserAgent;

$ua = LWP::UserAgent->new;

my $response = $ua->request(
    POST 'http://machine1/cgi-bin/myscript.pl',
    Content_Type => 'form-data',
    Content      => [ 
                        uploadthis => ["C:\myfile.txt"],
                    ]
    );

if ($response->is_success) {
    print "Success.";
}
else {
    print "Failure.";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文