大家好,抱歉我的英语不好。我想问这个问题,因为我很难在 Azure 文件存储(不是所谓的 Blob 存储)中上传文件(不是创建文件,而是上传)。 (当我尝试在 google chrome 浏览器上执行代码/运行时,没有发生错误。但是当我查看 azure 存储文件时,没有上传文件。)
所以基本上这是我的基本代码上传文件按钮。
<input type="file" class="btn btn-default" name="resFile" id="resFile" value="" />
<input type="submit" class="btn btn-default" value="Submit" data-inline="true"/>
<div id="res"></div>
这是我调用 ajax 请求的脚本。
$(document).ready(function () {
$("form").on('submit', (function (e) {
e.preventDefault();
$.ajax({
url: "functions/function.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
{
$("#res").hide().html(data).fadeIn('fast');
}
});
}));
});
最后是我的上传 php 代码:
<?php
require_once "../dependancies/vendor/autoload.php";
use MicrosoftAzure\Storage\Common\Exceptions\ServiceException;
use MicrosoftAzure\Storage\Common\Internal\Resources;
use MicrosoftAzure\Storage\Common\Internal\StorageServiceSettings;
use MicrosoftAzure\Storage\Common\Models\Range;
use MicrosoftAzure\Storage\Common\Models\Metrics;
use MicrosoftAzure\Storage\Common\Models\RetentionPolicy;
use MicrosoftAzure\Storage\Common\Models\ServiceProperties;
use MicrosoftAzure\Storage\Common\ServicesBuilder;
use MicrosoftAzure\Storage\Common\SharedAccessSignatureHelper;
use MicrosoftAzure\Storage\File\Models\CreateShareOptions;
use MicrosoftAzure\Storage\File\Models\ListSharesOptions;
use MicrosoftAzure\Storage\File\FileRestProxy;
$connectionString = 'DefaultEndpointsProtocol=https;AccountName=MYACCNAME;AccountKey=MYACCKEY==';
$fileClient = ServicesBuilder::getInstance()->createFileService($connectionString);
uploadFile($fileClient);
function uploadFile($fileClient)
{
$content = $_FILES["resFile"]["tmp_name"];
$contentloc = $_FILES["resFile"]["tmp_name"];
$shareName = 'myDISK';
$range = new Range(0, 511);
try {
$fileClient->createFileFromContentAsync($shareName, $contentloc, $content, null);
echo "<p style='color:green;'>File Uploaded successfully </p>";
} catch (ServiceException $e) {
$code = $e->getCode();
$error_message = $e->getMessage();
echo $code . ": " . $error_message . PHP_EOL;
}
}
?>
这是我尝试上传文件的图像:
[编辑] 我将其添加到我的 ajax 处理程序中
,
error: function (data)
{
console.log(data);
},
failure: function (data)
{
console.log(data);
}
[答案]
特别感谢@Aaron Chen 和@Gaurav Mantri。
只需添加 file_get_contents 即可获取文件本身。 将共享名更改为小写。 并且不使用“createFileFromContentAsync”,而是使用“createFileFromContent”
最佳答案
首先,您应该确保 Azure 文件存储中有一个名为 mydisk
的文件共享,然后尝试以下代码。我测试过,它应该可以工作。
function uploadFile($fileClient) {
// Get the content of the uploaded file
$content = file_get_contents($_FILES["resFile"]["tmp_name"]);
// Get uploaded file name
$filename = $_FILES["resFile"]["name"];
// Change the share name to mydisk (all lowercase)
$shareName = 'mydisk';
try {
// Use createFileFromContent instead of createFileFromContentAsync
$fileClient->createFileFromContent($shareName, $filename, $content, null);
echo "<p style='color:green;'>File Uploaded successfully </p>";
} catch (ServiceException $e) {
$code = $e->getCode();
$error_message = $e->getMessage();
echo $code . ": " . $error_message . PHP_EOL;
}
}
关于php - 使用php在azure文件存储(不是BLOB存储)中上传的正确方法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47494036/