1
votes

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:

click here to see 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'

1
Are you getting any error? Please edit your question and include that. - Gaurav Mantri
hi sir. there is no error found when i try to execute my code. but then when i look up at the azure storage files their is no file uploaded . - Yusoph Suma
` $fileClient->createFileFromContentAsync($shareName, $contentloc, $content, null);` i think this part of my code is working. but i dont really know where the files go in the server - Yusoph Suma
I'm not 100% sure but I believe you're getting the error because of share name. Please try 2 things: 1) Include error handler in your AJAX code. You should see some error there. and 2) Change the share name to mydisk (all lowercase). - Gaurav Mantri
hi sir i added the code after the success handler which is , error: function (data) { console.log(data); }, failure: function (data) { console.log(data); } but then nothing happens when i looked up at my console log. - Yusoph Suma

1 Answers

0
votes

First, you should make sure that you have a file share named mydisk in the Azure file storage, and then try the following code. I tested it, it should work.

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;
    }
}