How to rename uploaded files with additional data?

43 views
Skip to first unread message

Mati Roosalu

unread,
Mar 19, 2016, 9:08:44 AM3/19/16
to jQuery-File-Upload
I use jQuery-File_upload basic version for picture order module.

In main index.html file I add select:
<!-- The template to display files available for upload -->
<script id="template-upload" type="text/x-tmpl">
{% for (var i=0, file; file=o.files[i]; i++) { %}
    <tr class="template-upload fade">
        <td>
            <span class="preview"></span>
        </td>
        <td align="left">Vajadusel muuda kogust</p>
<select id="soflows" name="quantity[]">
  <option value="1P.C">1</option>
  <option value="2P.C">2</option>
  <option value="3P.C">3</option>
  <option value="4P.C">4</option>
  <option value="5P.C">5</option>
  <option value="6P.C">6</option>
  <option value="7P.C">7</option>
  <option value="8P.C">8</option>
  <option value="9P.C">9</option>
</select>
            <p class="name">{%=file.name%}</p>

In server/php/index.php I add rename function:

error_reporting(E_ALL | E_STRICT);
require('UploadHandler.php');

class CustomUploadHandler extends UploadHandler {
protected function handle_form_data($file, $index) {
        // Handle form data, e.g. $_POST['description'][$index]
//$amount = $_POST['quantity'][$index];
    }
protected function trim_file_name($file_path, $name, $size, $type, $error, $index, $content_range) {
$amount = $_REQUEST['quantity'][$index];
$name = $amount . '-' . $name;
return $name;
}
}

//$upload_handler = new UploadHandler();
$upload_handler = new CustomUploadHandler();

Typical scenario:

client add files 564.jpg; 565.jpg; 566... e.t.c and would like to order a different amount of photos

Client selects
564.jpg - quantity 2
565.jpg - quantity 3
566.jpg - quantity 1
etc.

I need result - new uploaded filenames 
2PC-564.jpg
3PC-565.jpg
1PC-566.jpg

Yeah, I get it but only in this case if client upload files with small button - "Start" and send all files separately.

If client use button "Start upload" to send them all at once I have problem.
With js/jquery.fileupload.js default options settings I have wrong result:
2PC-564.jpg
2PC-565.jpg
2PC-566.jpg

Can anyone recommend something?
Reply all
Reply to author
Forward
0 new messages