WWW::Mechanize : How to upload file under different file name?

1k views Asked by At

How to make WWW::Mechanize upload file under different file name?

I would like web server to see/record file name different from file name on my computer.

2

There are 2 answers

1
emazep On BEST ANSWER
my $file = [
    'filename-on-disk.txt', # The file you'd like to upload.
    'filename-for-upload.txt', # The filename you'd like to give the web server.
    'Content-type' => 'text/plain' # Any other flags you'd like to add go here.
];

$mech->post("http://example.com/upload.cgi", [
    'upload' => $file
]);

Taken from: https://gist.github.com/gaurav/253111#file-file-upload-pl

1
Joseph R. On

You can use a hard link:

link "file_name_on_your_computer","desired_new_name";
# Code to upload the newly created link
unlink "desired_new_name";

Notes

  • You need not worry about disk space usage if you're using a large file: the file is not duplicated, the hard link is simply a new name for the same inode.
  • This solution is limited to just those filesystems that support hard links. If you're on Windows, this may end up creating and uploading only a shortcut to your original file.