Php 试图从azure blob下载文件,mime类型出现问题
我有以下代码,在按下按钮时通过ajax调用下载文件。奇怪的是,它实际上工作正常,给了我预期的文件,有一段时间,当我再次访问以更改一些命名约定时,mime类型给了我一些问题。当下载时,它告诉我资源被解释为文档,但与应用程序/八位字节流一起传输。这些文件可以是pdf、图像或word文档,我想一种方法是检查扩展名并指定正确的mime类型,但是有没有更通用的方法,或者我可以从八进制流中获取原始文件Php 试图从azure blob下载文件,mime类型出现问题,php,azure,mime-types,azure-storage-blobs,Php,Azure,Mime Types,Azure Storage Blobs,我有以下代码,在按下按钮时通过ajax调用下载文件。奇怪的是,它实际上工作正常,给了我预期的文件,有一段时间,当我再次访问以更改一些命名约定时,mime类型给了我一些问题。当下载时,它告诉我资源被解释为文档,但与应用程序/八位字节流一起传输。这些文件可以是pdf、图像或word文档,我想一种方法是检查扩展名并指定正确的mime类型,但是有没有更通用的方法,或者我可以从八进制流中获取原始文件 try { // Get blob. $blob = $blobClient->
try {
// Get blob.
$blob = $blobClient->getBlob($container, $blob_name);
$properties = $blobClient->getBlobProperties($container, $blob_name);
$size = $properties->getProperties()->getContentLength();
$mime = $properties->getProperties()->getContentType();
header("Content-type: $mime");
header("Content-length: $size");
header ("Content-Disposition: attachment; filename=$blob_name");
fpassthru($blob->getContentStream());
}
您可以使用这些函数获取要下载的文件的mime类型
下面是示例代码片段
<?php
try {
// Get blob.
$blob = $blobClient->getBlob($container, $blob_name);
$properties = $blobClient->getBlobProperties($container, $blob_name);
$size = $properties->getProperties()->getContentLength();
$content = stream_get_contents($blob->getContentStream());
$finfo = new finfo(FILEINFO_MIME);
$mime = $finfo->buffer($content);
header("Content-type: $mime");
header("Content-length: $size");
header ("Content-Disposition: inline; filename=$blob_name");
print_r($content);
} catch(ServiceException $e) {
$code = $e->getCode();
$error_message = $e->getMessage();
echo $code.": ".$error_message."<br />";
}
此外,将文件上载到Azure存储的最佳做法是始终为blob设置内容类型,然后您可以使用上面提供的代码从Azure存储下载具有适当mime类型的文件
下面的代码示例演示了如何将blob上载到内容类型为的容器中:
<?php
require_once 'vendor/autoload.php';
use WindowsAzure\Common\ServicesBuilder;
use MicrosoftAzure\Storage\Blob\Models\CreateBlobOptions;
$connectionString = "DefaultEndpointsProtocol=https;AccountName=$account;AccountKey=$key";
$blobClient = ServicesBuilder::getInstance()->createBlobService($connectionString);
$filetoUpload = realpath('./image.jpg');
$content = fopen($filetoUpload, "r");
$mime = mime_content_type($filetoUpload);
$blob_name = "image.jpg";
try {
//Upload blob
$options = new CreateBlobOptions();
$options->setContentType($mime);
$blobClient->createBlockBlob("mycontainer", $blob_name, $content, $options);
}
catch(ServiceException $e) {
$code = $e->getCode();
$error_message = $e->getMessage();
echo $code.": ".$error_message."<br />";
}
谢谢,上传时添加mime类型就解决了这个问题。非常简单,一开始就不会因为某种原因想到要这么做!
<?php
require_once 'vendor/autoload.php';
use WindowsAzure\Common\ServicesBuilder;
use MicrosoftAzure\Storage\Blob\Models\CreateBlobOptions;
$connectionString = "DefaultEndpointsProtocol=https;AccountName=$account;AccountKey=$key";
$blobClient = ServicesBuilder::getInstance()->createBlobService($connectionString);
$filetoUpload = realpath('./image.jpg');
$content = fopen($filetoUpload, "r");
$mime = mime_content_type($filetoUpload);
$blob_name = "image.jpg";
try {
//Upload blob
$options = new CreateBlobOptions();
$options->setContentType($mime);
$blobClient->createBlockBlob("mycontainer", $blob_name, $content, $options);
}
catch(ServiceException $e) {
$code = $e->getCode();
$error_message = $e->getMessage();
echo $code.": ".$error_message."<br />";
}