Uploaded File Download Error

Uploaded File Download Error

by Amy Stewart -
Number of replies: 1

I am receiving an error when trying to download a file I uploaded in the configuration of a block that I built.  I am using the following code to save the file in my instance_config_save() function:

$draftitemid = $data->customdata;
$context = context_block::instance($this->instance->id);
file_save_draft_area_files($draftitemid, $context->id, 'block_tsvl', 'content', 0, array('subdirs'=>true, 'maxfiles'=>1));

I also have this function in my plugin to save the stored file:

function block_<plugin>_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options=array()) {
    global $CFG, $DB;
    //require_login($course, false, $cm);
    if ($filearea !== 'content') {
        return false;
    }
    $fs = get_file_storage();
    $relativepath = implode('/', $args);
    $fullpath = "/$context->id/block_<plugin>/$filearea/$relativepath";
    if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
        return false;
    }
    var_dump($options);
    // Finally send the file.
    send_stored_file($file, null, 0, $forcedownload, $options);    
}

To add a URL to the block for students to download the data, I am using the following code:

$downloadinfo = new stdClass();
$context = context_block::instance($instanceid);
 $fs = get_file_storage();
 $fileinfo = $DB->get_record('files', array('contextid'=>$context->id, 'component'=>'block_tsvl', 'filearea'=>'content',
  'itemid'=>0, 'mimetype'=>'application/zip'));
 $downloadinfo->url = moodle_url::make_pluginfile_url($context->id, 'block_tsvl', 'content',
                0, $fileinfo->filepath, $fileinfo->filename, true);

I then create a hyperlink from the $downloadinfo->url variable.  When I select the link, the file begins to download, but the download fails with a network error.  Our engineer insists it is something to do with my code, as downloading other files that have been uploaded to moodle appear to be working. 

Can anyone offer any insight as to why this may be occurring?  


Amy

Average of ratings: -
In reply to Amy Stewart

Re: Uploaded File Download Error

by Andreas Grabs -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Translators

Hi Amy,

I don't know whether your code is ok or not. But the "var_dump()" in your pluginfile method is definitely wrong.

You must not have any output but send_stored_file().

Best regards
Andreas