我在这里和其他地方阅读了许多解决方案,这些解决方案显示了如何将文件上传到 Azure Blob 存储。当文件被硬编码时,我让 php 代码在下面工作(遵循 MS 教程),但如果我尝试将文件发布到 PHP(得到错误:不是有效的路径),则不会。我希望用户能够浏览到表单中的文件并提交(然后返回 URL,以便我可以将其用于其他操作)。我在SO上读到javascript显示了一条虚假的安全路径,我认为这是我的问题。那么,如果我无法发布 PHP 文件,我如何获得它的正确路径。我已经尝试了默认帖子和ajax尝试。有解决办法吗?
我的PHP(在文件硬编码时工作):
require_once 'WindowsAzure'WindowsAzure.php';
use WindowsAzure'Common'ServicesBuilder;
use WindowsAzure'Common'ServiceException;
$connectionString = "DefaultEndpointsProtocol=https;AccountName=myaccount;AccountKey=mykey";
// Create blob REST proxy.
$blobRestProxy = ServicesBuilder::getInstance()->createBlobService($connectionString);
//$content = fopen("c:'myfile.txt", "r"); //this works when hard coded like this
//$blob_name = "myblob.txt";
//get posts
//$fpath = $_POST["resFile"];//tried this too - no go
$fpath = $_FILES["resFile"];
$fname = "hello.txt";
$content = fopen($fpath, "r"); //I know this isn't right, but trying
$blob_name = $fname;
try {
//Upload blob
$blobRestProxy->createBlockBlob("saskcontainer", $blob_name, $content);
}
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 />";
}
//and I need to return the url here on success
以下是 HTML 和 JS:
<form action="test.php" method="post" enctype="multipart/form-data">
<div data-role='page' id="resFileCont" >
<input type="file" name="resFile" id="resFile" value="" />
<!--<input type="text" name="name" />-->
<input type="submit" value="Submit" data-inline="true"/>
</div></form>
<div id="res"></div>
<script>
$(document).ready(function () {
$("form").on('submit', (function (e) {
e.preventDefault();
$.ajax({
url: "test.php", // Url to which the request is send
type: "POST", // Type of request to be send, called as method
data: new FormData(this), // Data sent to server, a set of key/value pairs (i.e. form fields and values)
contentType: false, // The content type used when sending data to the server.
cache: false, // To unable request pages to be cached
processData: false, // To send DOMDocument or non processed data file it is set to false
success: function (data) // A function to be called if request succeeds
{
alert(data)
$("#res").html(data)
}
});
}));
});
</script>
刚刚替换 $content = fopen($fpath, "r");
跟 $content = fopen($_FILES["resFile"]["tmp_name"], 'r');