2017-09-26 14 views
1

azuresブロブストレージを使用して以下を達成しようとしています。azure blobストレージにファイルをアップロードし、base64文字列のファイルでHTTP応答を送信しません。

  1. 空白のブロブストレージにファイルをアップロードします。
  2. 次に、ファイルのbase64文字列を含むhttp応答を送信します。

奇妙な部分は、自分のコードの順番に応じて他のものが動作を停止させるため、1つしか動作できないということです。

 HttpPostedFile image = Request.Files["froalaImage"]; 
     if (image != null) 
     { 
      string fileName = RandomString() + System.IO.Path.GetExtension(image.FileName); 
      string companyID = Request.Form["companyID"].ToLower(); 

      // Retrieve storage account from connection string. 
      CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
       CloudConfigurationManager.GetSetting("StorageConnectionString")); 

      // Create the blob client. 
      CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); 

      // Retrieve reference to a previously created container. 
      CloudBlobContainer container = blobClient.GetContainerReference(companyID); 

      // Create the container if it doesn't already exist. 
      container.CreateIfNotExists(); 

      // Retrieve reference to a blob named "filename". 
      CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName); 

      // Create or overwrite the blob with contents from a local file. 
      using (image.InputStream) 
      { 
       blockBlob.UploadFromStream(image.InputStream); 
       byte[] fileData = null; 
       using (var binaryReader = new BinaryReader(image.InputStream)) 
       { 
        fileData = binaryReader.ReadBytes(image.ContentLength); 
       } 
       string base64ImageRepresentation = Convert.ToBase64String(fileData);     

       // Clear and send the response back to the browser. 
       string json = ""; 
       Hashtable resp = new Hashtable(); 
       resp.Add("link", "data:image/" + System.IO.Path.GetExtension(image.FileName).Replace(@".", "") + ";base64," + base64ImageRepresentation); 
       resp.Add("imgID", "BLOB/" + fileName); 
       json = JsonConvert.SerializeObject(resp); 
       Response.Clear(); 
       Response.ContentType = "application/json; charset=utf-8"; 
       Response.Write(json); 
       Response.End(); 
      } 
     } 

上記のコードは、ファイルをazureのBLOBストレージにアップロードしますが、base64文字列は空になります。

しかし、私はblockBlob.UploadFromStream(image.InputStream);ラインstring base64ImageRepresentation = Convert.ToBase64String(fileData);

以下の行を置く場合、私は何の問題はしかし、ファイルが紺碧のブロブストレージに正しくアップロードされていないされていないbase64文字列を取得します。

答えて

5

最初の使用後にストリームの位置をリセットする必要がありますか?

image.InputStream.Seek(0, SeekOrigin.Begin); 
+0

ありがとうございました! –

関連する問題