2014-09-03 38 views
1

我想使用Azure存儲中的文件名將本地計算機上的Azure存儲塊作爲文件下載並保存。我在這裏使用了一個名爲download.php的文件(tutorial link),當我去download.php時,我可以在瀏覽器中看到文件內容。如果我然後把index.php中的鏈接放到download.php中,我可以右鍵單擊鏈接並「另存爲」,並將文件另存爲myfile.doc。然後我可以成功打開myfile.doc。如何使用PHP將Azure存儲blob下載爲本地文件?

的index.php:

echo '<a href="http://myserver/dev/download.php" >Click me</a>'; 

不過...... 我想知道的是如何得到的鏈接「另存爲」,而不必點擊右鍵用戶。此外,我有文件名(來自Azure存儲) - 但我不知道如何使用該文件名來保存文件。當用戶點擊鏈接時,如何使用文件名將文件保存到用戶的下載目錄?

回答

1

要做到這一點,我改變的index.php到窗體:

echo '<form method="post" action="download.php"><div id="divexport">'; 
    echo '<input type="hidden" name="Export" value="coverLetter">'; 
    echo '<input type="submit" id="Export" value="Cover Letter" />'; 
    echo '</div></form>'; 

,然後添加的頭信息的download.php,只是fpassthru之前

// Create blob REST proxy. 
$blobRestProxy = ServicesBuilder::getInstance()->createBlobService($connectionString); 
$blobfile = "myblob.pdf"; 
$filename = basename($blobfile); 
$ext = new SplFileInfo($filename); 
$fileext = strtolower($ext->getExtension()); 

try { 
    // Get blob. 
    $blob = $blobRestProxy->getBlob("document", $blobfile); 

    if($fileext === "pdf") { 
     header('Content-type: application/pdf'); 
    } else if ($fileext === "doc") { 
     header('Content-type: application/msword'); 
    } else if ($fileext === "docx") { 
     header('Content-type: application/vnd.openxmlformats-officedocument.wordprocessingml.document'); 
    } else if($fileext === "txt") { 
     header('Content-type: plain/text'); 
    } 
    header("Content-Disposition: attachment; filename=\"" . $filename . "\""); 
    fpassthru($blob->getContentStream()); 
} 
catch(ServiceException $e){ 
    // Handle exception based on error codes and messages. 
    // Error codes and messages are here: 
    // http://msdn.microsoft.com/en-us/library/windowsazure/dd179439.aspx 
    $code = $e->getCode(); 
    $error_message = $e->getMessage(); 
    echo $code.": ".$error_message."<br />"; 
} 
相關問題