I can’t download a file which was uploaded with a filepicker

I can’t download a file which was uploaded with a filepicker

by Jenő Duchon -
Number of replies: 4

Hi,

I try to learn how can build a block under moodle 2.x so do everything from the 2 tutorial (Basic, Advenced). But I have a big question. The tutorial make a form with filepicker but not it show, how can i download in a viewpage this file.

I tried download it with File API. I writed this code:

$fs = get_file_storage();
//$task->reportedid content that userid who send a form another page like in the tutorial
$context = get_context_instance(CONTEXT_USER, $task->reporterid);
$files = $fs->get_area_files($context->id, 'user', 'draft', $task->filename);
foreach ($files as $file) {
  $filename = $file->get_filename();
  $url = moodle_url::make_pluginfile_url($file->get_contextid(), $file->get_component(), $file->get_filearea(), $file->get_itemid(), $file->get_filepath(), $file->get_filename(), TRUE);  $out = html_writer::link($url, $filename); echo $out; }

The generated url: http://localhost/moodle/pluginfile.php/5/user/draft/353424072/Desert.jpg?forcedownload=1

but the Moodle says, there is not a file. L

How can I do that: somebody send a form with a file (like the tutorial), and other person who watch the sended data in a table can download it via a link.

Thanks your answer

Average of ratings: -
In reply to Jenő Duchon

Re: I can’t download a file which was uploaded with a filepicker

by David Mudrák -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Plugins guardians Picture of Testers Picture of Translators

The 'draft' filearea is just what the name says - a temporary area used during the file uploading. When the form with files picked via the filepicker is submitted, the processing code is responsible for storing the files in their final locations. Typically, this is what the file_save_draft_area_files() function does. One should not rely on accessing the files in the draft area, it's mainly for the filepicker internal purposes.

Average of ratings: Useful (2)
In reply to David Mudrák

Tárgy: Re: I can’t download a file which was uploaded with a filepicker

by Jenő Duchon -

Thanks your answer.

This is very logical, but unfortunately I can't do it because I didn't find examples. The tutorial doesn't say who can i do it L

Now, I update my code to filemanager J I think move 1 step but not it was not enought.

This is my code parts:

  • addtask_form.php:
      $mform->addElement('filemanager', 'filename', get_string('file'), null, array('subdirs' => 0, 'maxfiles' =>
10, 'accepted_types' => '*'));

  • addtask.php:
if($addtask->is_cancelled()) {
   redirect($courseurl);
} else if($fromform = $addtask->get_data()) { 
    $newtaskDB = new stdClass();
    $newtaskDB->name = $fromform->name;
    $newtaskDB->description = $fromform->description['text'];
    $newtaskDB->groupid = $fromform->groupid;
    $newtaskDB->priority = $fromform->priority;
    $newtaskDB->filename = $fromform->filename;
    $newtaskDB->reporterid = $USER->id;
    $newtaskDB->taskpercent = 0;
    $newtaskDB->opendate = time();
    if (!$taskid = $DB->insert_record('block_taskblock', $newtaskDB)) {
       print_error('addnewtaskerror', 'block_taskblock');
    }
if ($draftitemid = file_get_submitted_draft_itemid('filename')) {
    file_save_draft_area_files($draftitemid, $context->id, 'block_taskblock', 'filename', $taskid, array('subdirs' => false, 'maxfiles' => 10));
}
   redirect($courseurl);
}

 

  • viewtask.php:
$fs = get_file_storage();
$context =get_context_instance(CONTEXT_BLOCK, $blockid);
$files = $fs->get_area_files($context->id, 'block_taskblock', 'filename');
foreach ($files as $file) {
        $filename = $file->get_filename();
        $url = moodle_url::make_pluginfile_url($file->get_contextid(), $file->get_component(), $file->get_filearea(), $file->get_itemid(), $file->get_filepath(), $file->get_filename(), TRUE);
        $out = html_writer::link($url, $filename);
        echo $out;
}

The URL is: http://localhost/moodle/pluginfile.php/35/block_taskblock/filename/36/Desert.jpg?forcedownload=1

But Moodle says there isn't file L

the mdl_files recordpart:

  • contextid: 35
  • component: block_taskblock
  • fileare: filename
  • filepath: /
  • filename: Desert.jpg

In reply to Jenő Duchon

Re: Tárgy: Re: I can’t download a file which was uploaded with a filepicker

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Have you created a function:

block_taskblock_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options=array()) 

inside blocks/taskblock/lib.php?

If not, there are several examples throughout core code (forum_pluginfile is as good as any other to look at).

Basically you need to check the file request, confirm that the user has permission to access this particular file and then call send_stored_file to return it to their browser (otherwise the user could just type random URLs and see what it would let them download).



Average of ratings: Useful (2)
In reply to Davo Smith

Tárgy: Re: Tárgy: Re: I can’t download a file which was uploaded with a filepicker

by Jenő Duchon -

Thanks your answer! 

The lib.php was missing. Its works :D