开发者

how to get file name on perl of windows popup?

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> clas开发者_开发技巧s="loading">uploading...</span>');
    },
    onComplete: function(file, response) {
      $("#upload_attachment_button").removeClass('ui-state-disabled');
      alert(response);
    }
});


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜