Hello guys sorry for my bad English. i would like to ask this question because i am having a hard time uploading a file (not to create file but upload) in Azure File Storage(NOT THE SO CALLED BLOB STORAGE). (no error happens when i try to execute the code/run on the google chrome browser. but then when i look up at the azure storage files there is no file uploaded .)
so basically this is my base code upload file button.
<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>
then this is my script to call an ajax request.
$(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');
}
});
}));
});
and finally my php code for uploading:
<?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;
}
}
?>
This is the image on where i try to upload the file:
[EDIT]
i added this on my ajax handler
,
error: function (data)
{
console.log(data);
},
failure: function (data)
{
console.log(data);
}
[ANSWER]
special thanks to @Aaron Chen and @Gaurav Mantri.
just add file_get_contents to get the file itself. Change the share name to lowercase. and instead of using 'createFileFromContentAsync' use 'createFileFromContent'
mydisk(all lowercase). - Gaurav Mantri