如何在Windows弹出窗口的perl上获取文件名?

发布于 2024-09-24 22:46:47 字数 979 浏览 7 评论 0原文

我正在使用 json 打开用户弹出窗口。 我曾经在 php 上使用 basename( $_FILES['userfile']['name'] ) ,如何在 perl 上做到这一点?

服务器端代码:

#!/usr/bin/perl
use CGI;

print "Content-type: text/html; 
Cache-Control: no-cache;
charset=utf-8\n\n";

@allowedExtensions =("jpg","tiff","gif","eps","jpeg","png");

my $q = CGI->new();

my $filename = $q->upload('userfile');

print "file name is $file_name";

客户端代码:

var post_obj = new Object();

new AjaxUpload('upload_attachment_button', {
    action: 'upload.cgi',
    type: "POST",
    data: post_obj,

    onChange: function() {},
    onSubmit: function() {
      $("#upload_attachment_button").addClass('ui-state-disabled');
      $("#upload_proj_message").html('<span> class="loading">uploading...</span>');
    },
    onComplete: function(file, response) {
      $("#upload_attachment_button").removeClass('ui-state-disabled');
      alert(response);
    }
});

I'm using json to open the user popup.
I used to use basename( $_FILES['userfile']['name'] ) on php, how to do that on perl?

Server side code:

#!/usr/bin/perl
use CGI;

print "Content-type: text/html; 
Cache-Control: no-cache;
charset=utf-8\n\n";

@allowedExtensions =("jpg","tiff","gif","eps","jpeg","png");

my $q = CGI->new();

my $filename = $q->upload('userfile');

print "file name is $file_name";

Client side code:

var post_obj = new Object();

new AjaxUpload('upload_attachment_button', {
    action: 'upload.cgi',
    type: "POST",
    data: post_obj,

    onChange: function() {},
    onSubmit: function() {
      $("#upload_attachment_button").addClass('ui-state-disabled');
      $("#upload_proj_message").html('<span> class="loading">uploading...</span>');
    },
    onComplete: function(file, response) {
      $("#upload_attachment_button").removeClass('ui-state-disabled');
      alert(response);
    }
});

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

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

发布评论

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

评论(1

清晨说晚安 2024-10-01 22:46:47

看起来您正在尝试获取用户上传的文件的名称。如果您使用的是 CGI 模块,那么这里是解决方案:

use CGI;
my $q = CGI->new();

my $filename = $q->param('userfile'); ## retrive file name of uploaded file

来自 手册

不同的浏览器将返回稍微不同的名称。某些浏览器仅返回文件名。其他人使用用户计算机的路径约定返回文件的完整路径。无论如何,返回的名称始终是用户计算机上文件的名称,与 CGI.pm 在上传假脱机期间创建的临时文件的名称无关(见下文)。

更新:

抱歉,之前没注意到。请在脚本开头添加use strict;。它会强制您声明所有变量。您会在 print 语句中看到错误:

print "file name is $filename"; ## must be $filename

要声明 @allowedExtensions 只需在首次使用之前添加 my

my @allowedExtensions =("jpg","tiff","gif","eps","jpeg","png");

另外我相信您不需要打印 HTTP 标头时在行尾添加 ;

print "Content-type: text/html 
Cache-Control: no-cache
charset=utf-8\n\n";

并且请始终使用 strict。它将在未来为您节省大量时间。

Looks like you trying to get name of the file uploaded by user. If you are using CGI module, then here is solution:

use CGI;
my $q = CGI->new();

my $filename = $q->param('userfile'); ## retrive file name of uploaded file

From the manual:

Different browsers will return slightly different things for the name. Some browsers return the filename only. Others return the full path to the file, using the path conventions of the user's machine. Regardless, the name returned is always the name of the file on the user's machine, and is unrelated to the name of the temporary file that CGI.pm creates during upload spooling (see below).

Update:

Sorry, didn't noticed it before. Please add use strict; at the begining of the script. It'll force you to declare all variables. You'll see mistype in print statement:

print "file name is $filename"; ## must be $filename

To declare @allowedExtensions just add my before first use:

my @allowedExtensions =("jpg","tiff","gif","eps","jpeg","png");

Also I believe that you don't need ; at the end of lines when you print HTTP headers:

print "Content-type: text/html 
Cache-Control: no-cache
charset=utf-8\n\n";

And please always use strict. It'll save you tons of time in future.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文