如何使用cURL发送文件

问题描述 投票:0回答:1

我有一个简单的 perl cgi 脚本,它可以显示一个网页以允许选择文件,或者如果提交了文件,则接受该文件并向浏览器显示内容。

它通过浏览器运行良好。我如何使用curl来模拟这个?

我尝试的方法不起作用:curl -X POST -F [电子邮件受保护] myserver.com/cgi-bin/script.cgi

这是 Perl 脚本:

#!/usr/bin/perl
use CGI;

# Create CGI object
my $cgi = CGI->new;

# Print Content-Type header
print $cgi->header('text/html');

# Check if a file has been uploaded
if ($cgi->param('upload')) {
    # Get the uploaded file handle
    my $file_handle = $cgi->upload('file_input');

    # Read and display the content of the file
    if ($file_handle) {
        print "<h1>File Content</h1>";

        # Slurp the entire content of the file
        my $file_content = do { local $/; <$file_handle> };

        # Display the content
        print "<pre>$file_content</pre>";
    } else {
        print "<p>Error reading the uploaded file.</p>";
    }
} else {
    # Display the file upload form
    print <<HTML;
<!DOCTYPE html>
<html>
<head>
    <title>File Upload Form</title>
</head>
<body>
    <h1>File Upload Form</h1>
    <form method="post" enctype="multipart/form-data">
        <label for="file_input">Choose a file:</label>
        <input type="file" name="file_input" id="file_input" required>
        <br>
        <input type="submit" name="upload" value="Upload">
    </form>
</body>
</html>
HTML
}
perl curl
1个回答
0
投票

您缺少程序所需的

upload
参数。

if ($cgi->param('upload')) {

您的表单有一个发送

upload=Upload
的按钮。

<input type="submit" name="upload" value="Upload">

但是你没有用curl发送它。

© www.soinside.com 2019 - 2024. All rights reserved.