在Azure Blob Storage

时间:2016-06-10 17:35:43

标签: php azure blob azure-sdk-php

我正在尝试使用Azure PHP SDK将图像从HTML表单上传到Azure Blob存储。我尝试下载图像时出现问题。结果页面可以在帖子的底部看到。

我使用临时名称存储图像,我认为这是两个问题之一。我不确定,但第二个问题是下载图像时。我是否必须将它从getContentStream()转换为图像?

  

$ _ FILES [ 'driverLicenseFront'] [ 'tmp_name的值']

这是上传图片的html表单:

<form role="form" method="POST" action="{path_to_controller}" data-toggle="validator" enctype="multipart/form-data">
    <div class="form-group">
        <label for="driverLicenseFront">Upload Driver's License(Front)</label>
        <input type="file" id="driverLicenseFront" name="driverLicenseFront">
    </div>
    <submit button>
</form>

在控制器中我存储文件如下:

// First check if there is a container
$blob = New Blob($_SESSION['userid']);
$blob->createContainerIfNotExists();
// Upload image to Azure Blob Storage
$content = fopen($_FILES['driverLicenseFront']['tmp_name'].'', "r");
$blob->uploadToContainer($content,'DriverLicenseFrontSide');
  

Blob 是我处理blob的自定义类

我需要使用以下链接下载文件:

<a href="../controller/blobs.php?blob_name=DriverLicenseFrontSide" target="_new">Download</a>

我在控制器中捕获了请求:

if(isset($_GET['blob_name'])){
    $blob = New Blob($_SESSION['userid']);
    $blob->downloadBlob($_GET['blob_name']);
} 

Blob类的功能:

public function downloadBlob($blob_name){
    try {
        // Get blob.
        $blob = $this->blobRestProxy->getBlob($this->containerName, $blob_name.'.jpg');
        fpassthru($blob->getContentStream());
    }
    catch(ServiceException $e){
        // Handle exception based on error codes and messages.
        // Error codes and messages are here:
        // http://msdn.microsoft.com/library/azure/dd179439.aspx
        $code = $e->getCode();
        $error_message = $e->getMessage();
        echo $code.": ".$error_message."<br />";
    }
}

结果:

  

JFIF,, ICC_PROFILE mntrRGBXYZ    $acsp -) =ޯ U xB ʃ9descDybXYZ bTRC dmdd gXYZ   hgTRC lumi| meas $ bkpt rXYZ rTRC tech vued    wtptpcprt 7chad ,descsRGB IEC61966-2-1 black scaledXYZ   $ curv#( - 27; @ EJOTY ^ chmrw | ...

1 个答案:

答案 0 :(得分:0)

您似乎忘记添加content-type响应以将二进制内容转换为图片内容。

尝试在downloadBlob()功能中添加以下代码。

$blob = $this->blobRestProxy->getBlob($this->containerName, $blob_name.'.jpg');
header("Content-Type:image/jpeg");
header('Content-Disposition: attachment; filename="' . $blob_name . '"');
fpassthru($blob->getContentStream());

如有任何疑问,请随时告诉我。

相关问题