Download a file

@param $file_path
@param $file_name
@param $response
196672
public static function download($file_path, $file_name, ResponseContract $response = null)
    {
        if (file_exists(iconv('UTF-8', 'GB2312', $file_path))) {
            $file_size = filesize($file_path);
            $fp = fopen($file_path, self::READ_BINARY);
            self::header('Content-type', 'application/octet-stream', $response);
            self::header('Accept-Ranges', 'bytes', $response);
            self::header('Accept-Length', $file_size, $response);
            self::header('Content-Disposition', 'attachment; filename=' . $file_name, $response);
            self::header('Expires', '-1', $response);
            self::header('Cache-Control', 'no_cache', $response);
            self::header('Pragma', 'no-cache', $response);
            //兼容IE11
            $ua = Lb::app()->getUserAgent();
            $encoded_filename = urlencode($file_name);
            if(preg_match("/MSIE/is", $ua) || preg_match(preg_quote("/Trident/7.0/is"), $ua)) {
                self::header('Content-Disposition', 'attachment; filename="' . $encoded_filename . '"', $response);
            } else if (preg_match("/Firefox/", $ua)) {
                self::header('Content-Disposition', 'attachment; filename*="utf8\'\'' . $file_name . '"', $response);
            } else {
                self::header('Content-Disposition', 'attachment; filename="' . $file_name . '"', $response);
            }
            echo fread($fp, $file_size);
            fclose($fp);
            exit;
        }
    }
1276958
public void download(String path, String fileName, File outFile) {
		if (outFile.isDirectory()) {
			outFile = new File(outFile, fileName);
		}
		if (false == outFile.exists()) {
			FileUtil.touch(outFile);
		}
		try (OutputStream out = FileUtil.getOutputStream(outFile)) {
			download(path, fileName, out);
		} catch (IOException e) {
			throw new FtpException(e);
		}
	}
832441
public function download($date, $filename = null, $headers = [])
    {
        if (is_null($filename)) {
            $filename = "laravel-{$date}.log";
        }

        $path = $this->filesystem->path($date);

        return response()->download($path, $filename, $headers);
    }
1346924
function load_file($file_name, $prepend_path = true)
	{
		if ($prepend_path && $this->_config_path != "")
		$config_file = $this->_config_path . $file_name;
		else
		$config_file = $file_name;

		ini_set('track_errors', true);
		$fp = @fopen($config_file, "r");
		if (!is_resource($fp)) {
			$this->_trigger_error_msg("Could not open config file '$config_file'");
			return false;
		}

		$contents = ($size = filesize($config_file)) ? fread($fp, $size) : '';
		fclose($fp);

		$this->_config_data[$config_file] = $this->parse_contents($contents);
		return true;
	}
188630
public function file(string $file, string $name = null, bool $download = true): ResponseInterface
    {
        if (!\is_file($file)) {
            throw new \LogicException("File '$file' doesn't exist.");
        }

        $extension = \substr($file, \strrpos($file, '.') + 1);
        $this->header->set('Content-Type',
            MimeType::getMimeType($extension) ?? 'application/octet-stream'
        );

        $disposition = $download ? Header::DISPOSITION_ATTACHMENT : Header::DISPOSITION_INLINE;
        $this->header->set('Content-Disposition',
            $this->header->makeDisposition($disposition, $name, \basename($file))
        );

        $size = $length = \filesize($file);

        $this->header->set('Accept-Ranges', 'bytes');
        if (\preg_match('#^bytes=(\d*)-(\d*)\z#', $this->request->header('Range'), $matches)) {
            list(, $start, $end) = $matches;

            if ($start === '') {
                $start = \max(0, $size - $end);
                $end = $size - 1;
            } elseif ($end === '' || $end > $size - 1) {
                $end = $size - 1;
            }

            if ($end < $start) {
                return $this->empty(416); // requested range not satisfiable
            }

            $this->setStatus(206);
            $this->header->set('Content-Range', 'bytes ' . $start . '-' . $end . '/' . $size);
            $length = $end - $start + 1;
        } else {
            $this->header->set('Content-Range', 'bytes 0-' . ($size - 1) . '/' . $size);
        }

        $this->header->set('Content-Length', $length);

        $this->app->emitter(
            $this->emitter ?? Emitter\SapiStream::class
        );

        return $this->response(
            Factory::streamFromFile($file, 'rb')
        );
    }
183780
public function DownloadAction(Request $request, $path)
    {
        /** @var FileManagerService $service */
        $service = $this->get('youwe.file_manager.service');
        $service->checkToken($request->get('token'));

        /** @var FileManagerDriver $driver */
        $driver = $this->get('youwe.file_manager.driver');
        $parameters = $this->container->getParameter('youwe_file_manager');
        $fileManager = $service->createFileManager($parameters, $driver);

        $filepath = $fileManager->getPath($path, null, true);
        $content = file_get_contents($filepath);
        $filename = basename($path);
        $response = new Response();

        $response->headers->set('Content-Type', 'mime/type');
        $response->headers->set('Content-Disposition', 'attachment;filename="' . $filename . '"');
        $response->setContent($content);

        return $response;
    }
316457
public function downloadFile($path = '', $destination = '', $name = '')
    {
        $response = $this->sendRequest('GET', $path);
        if ($name === '' && $response->getHeader('Content-Disposition')
            && is_array($response->getHeader('Content-Disposition'))
            && count($response->getHeader('Content-Disposition')) > 0
        ) {
            $matchResults = [];
            preg_match(
                "/.*?filename=\"(.*?)\".*?/",
                $response->getHeader('Content-Disposition')[0],
                $matchResults
            );
            $name = urldecode($matchResults[1]);
        }

        $file = $destination . $name;

        $result = file_put_contents($file, $response->getBody()) ? $file : false;

        return $result;
    }
1257211
public function readFile($path, $file_handle = NULL) {/*{{{*/
        return $this->_do_request('GET', $path, NULL, NULL, $file_handle);
    }
1008498
public function b2_download_file_by_name($download_url, $bucket_name, $file_name, $auth_token = NULL)
        {
            $call_url     = $download_url."/file/".$bucket_name."/".$file_name;
            $auth_token   = $auth_token; // Obtained from the b2_authorize_account call
            $download_url = $download_url; // Obtained from the b2_authorize_account call
            $bucket_name  = $bucket_name; // The name of the bucket you wish to download from
            $file_name    = $file_name; // The name of the file you wish to download

            // Add headers
            $headers = array(
                "Authorization: {$auth_token}"
            );

            $result = b2_call($call_url, $headers);
            return $result; // Return the result
        }
241358
def download(url, path)
      if url.match(FILE_URL)
        FileUtils.cp(url.sub(FILE_URL, ''), path)
      else
        File.open(path, 'w+') do |file|
          Net::HTTP.get_response(URI(url)) do |response|
            response.read_body do |chunk|
              file.write chunk
            end
          end
        end
      end
      path
    end
553177
public static function download($path = false, $extension = false, $name = false) {

        if($path && File::isFile($path)) {

            if(!$extension) {
                $extension = File::extension($path);
            }

            if(!$name) {
                $name = basename($path);
            }

            header('Content-Type: application/' . $extension);
            header("Content-Transfer-Encoding: Binary");
            header("Content-disposition: attachment; filename=" . $name);
            readfile($path);
            exit();
        }

        return false;
    }
385597
static function includeFile($paths, $file_name) {
        $path_arr = explode(';', $paths);
        foreach ($path_arr as $p) {
            $file = $p . '/' . $file_name;
            if (file_exists($file)){
                return include $file;
            }
        }
        throw new FileNotExistException("unable to include the file $file_name");
    }
1096150
def file_download(object_id, input_params={}, always_retry=True, **kwargs):
    
    return DXHTTPRequest('/%s/download' % object_id, input_params, always_retry=always_retry, **kwargs)
169337
def download_file_helper(url, input_path):
    
    r = requests.get(url, stream=True)
    if r.status_code != 200:
        cli_log.error("Failed to download file: %s" % r.json()["message"])
    local_full_path = get_download_dest(input_path, r.url)
    original_filename = os.path.split(local_full_path)[-1]
    with open(local_full_path, "wb") as f:
        click.echo("Downloading {}".format(original_filename), err=True)
        for chunk in r.iter_content(chunk_size=1024):
            if chunk:  # filter out keep-alive new chunks
                f.write(chunk)
                f.flush()
    pprint("Successfully downloaded %s to %s" % (original_filename, local_full_path), True)
1035967
public static void setFileDownloadHeader(final HttpServletResponse response, final String filename) {
		// 中文文件名支持
		String encodedFilename = Encodes.urlEncode(filename);
		response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + encodedFilename + '\"');
	}
86500
public static function readFile($file_path)
    {
        try {
            $file = fopen($file_path, 'r');
            $content = fread($file, filesize($file_path));
            fclose($file);
            return $content;
        } catch (\Exception $e) {
            return '';
        }
    }
191461
def get_file_download(self, file_id):
        
        return self._create_item_response(
            self.data_service.get_file_url(file_id),
            FileDownload
        )