【问题标题】:perl cgi passing filehandleperl cgi 传递文件句柄
【发布时间】:2015-05-10 00:07:45
【问题描述】:

我有 perl cgi 脚本和上传文件的表单。 一切正常,但如果已存在同名文件,则在用户选择文件后我有一些控制(检查我的 SQL 表)。 如果存在,我打印 javascript 确认询问用户是否要替换旧文件。 而且我不知道如何将文件句柄传递给脚本。我的upload.pl代码:

my $query = new CGI;  
my $file = $query->param("file");
my $confirm = $query->param("confirm"); 
my $name = $query->param("name");

if(!$confirm){
  SQL check
  if(SQL check){
    print "Content-type: text/html\n\n";
    print "<form action=\"upload.pl\" method=post name=\"upload.pl\">
       <input type=\"hidden\" name=\"file\" value=\"$file\">
       <input type=\"hidden\" name=\"confirm\" value=\"true\">
       <input type=\"hidden\" name=\"name\" value=\"$name\">
     </form>
     <form action=\"index.pl\" method=GET name=\"index.pl\">
       <input type=\"hidden\" name=\"login\" value=\"$login\">
       <input type=\"hidden\" name=\"token\" value=\"$token\">
     </form>
     <script>
       var answer = confirm(\"WARNING .....\"); 
       if (answer==true){
         document.forms[\"upload.pl\"].submit();
       }
       else{
         document.forms[\"index.pl\"].submit();            
       }
     </script>
     ";
  exit;
  }  
}

所以在用户确认他想要替换文件后,我提交第一个表单并再次运行upload.pl,但是当我尝试时

my $upload_filehandle = $query->upload("file");

它什么也不返回。

有没有办法传递文件句柄? 或者其他没有javascript确认并再次提交脚本的方式?

感谢您的建议。

【问题讨论】:

    标签: javascript perl upload cgi


    【解决方案1】:
    $query->upload("file")
    

    用于文件类型输入(例如&lt;input type="file" name="file"&gt;)。对于其他类型的输入,请使用

    $query->param("file")
    

    如果你希望能够从文件句柄中读取$query-&gt;param("file")返回的值,你可以使用

    open(my $fh, '<', \$query->param("file"))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-12
      • 2015-04-13
      • 2012-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多