douyong1885 2018-11-24 02:49
浏览 56

无法使用MS-Azure Blob存储中的文件路径上载映像

I am developing a web application using Microsoft Azure services. I am following this link to upload an image to my blob storage, but there are a few bits and pieces missing in the document that leads to certain issues. For instance, how do we define the variable $fileToUpload and how will it relate to the variable $myfile? Also, if we want to choose to upload a file from our system when the application is running, in which of the two variables should we have the absolute file path to my image? The code I have written till now to upload an image is as follows:

function imageupload(string $current_user){
    $pt_id = $_POST['patientid'];
    if (strcmp($pt_id,"")==0){
        echo "Select PatientID to continue";
    }else{
        $accountexists = load($current_user, $pt_id);
        if ($accountexists == 0){
            $error = "Patient Id does not exist";
            echo $error;
        }else{
            $img_path = $_POST['uploadI'];
            if (strcmp($img_path, "")==0){
                 echo "Enter valid image path <br />";
            }
            else{
                require_once 'vendor/autoload.php';
                $connectionString = '****';
                // Create blob client.
                $blobClient = BlobRestProxy::createBlobService($connectionString);

                # Create the BlobService that represents the Blob service for the storage account

                $containerName = $current_user;

                $num = rand(10,10000);
                $num2 = (string) $num;
                $fileToUpload = $pt_id."_".$num2;

                # Upload file as a block blob
                echo "Uploading image: ".PHP_EOL;
                echo $img_path;
                echo "<br />";


                $content = fopen($img_path, "r");

                //Upload blob
                $blobClient->createBlockBlob($containerName, $fileToUpload, $content);
                echo "Image uploaded successfully! <br />";
            }
        }
    }
}

In this code, I am storing my path to the image in the variable $img_path. And this is the code I am using for displaying the image later:

// Gets all the images from the hospital's container and filters out the images corresponding to a particular patient. 
// For the patient, the image would be saved on cloud with name as: <patientid>_<some random number>
// Eg- patient id= pt1 , so image name could be- pt1_3682
function uploadedimage(string $current_user, string $HospitalId, string $PatientId){
  $containerName = $HospitalId;
  $url = "";
  try{
      // Get blob.
      require_once 'vendor/autoload.php';
      // use MicrosoftAzure\Storage\Common\ServicesBuilder;
      $connectionString = '****';

      $blobClient = BlobRestProxy::createBlobService($connectionString);
      $blob_list = $blobClient->listBlobs($containerName);
      $blobs = $blob_list->getBlobs();
      $strlen = strlen($PatientId);
      $array_user = array();
      // echo "These are the blobs present in the container: ";
      foreach($blobs as $blob)
      {
          $name =  $blob->getName();
          $namesub = substr($name,0, $strlen);
          if (strcmp($namesub, $PatientId) == 0){
              array_push($array_user, $blob);
          }
      }
  }
  catch(ServiceException $e){
    $code = $e->getCode();
    $error_message = $e->getMessage();
    echo $code.": ".$error_message."<br />";
  }

  $url = "";
  try    {
      foreach($array_user as $blob)
      {
          $name =  $blob->getName();
          $url = $blob->getUrl();
          echo "<img src=\"".$url."\" alt=\"image post\"></br></br>";

      }

  }
  catch(ServiceException $e){
      $code = $e->getCode();
      $error_message = $e->getMessage();
      echo $code.": ".$error_message."<br />";
  }
}

But I cannot seem to view my image in the html that I have specified. All I get is the default text "image post". Thus I have concluded there is something I am missing in my code due to which the images are not being uploaded but I can't seem to figure that out from the azure tutorial.

Edit: I checked my blob properties and they are all of 0 size, which means that they are not being uploaded properly. Also I realised that there are some images that were pushed to the cloud along with my code in my git folder. So if there is a particular image by the name abc.png already existing in the same directory as my code on Azure and while uploading this image from my web application, if in the path text box I specify only abc.png, that image gets uploaded in my container in it's correct size, but if that same image is present on my desktop and if I specify the complete filepath of my local system for this image (or some other image not present in the git directory existing on azure) as C:/Desktop/abc.png, it gets uploaded as only 0 bytes image (which is essentially not being uploaded properly). What can be the issues if it is not able to take images from my system and can only take the images already existing in it's git directory?

展开全部

  • 写回答

0条回答 默认 最新

      编辑
      预览

      报告相同问题?

      手机看
      程序员都在用的中文IT技术交流社区

      程序员都在用的中文IT技术交流社区

      专业的中文 IT 技术社区,与千万技术人共成长

      专业的中文 IT 技术社区,与千万技术人共成长

      关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

      关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

      客服 返回
      顶部