c# - 将文件上传到 azure blob 存储,然后使用文件的 base64 字符串发送 http 响应不起作用

标签 c# .net azure azure-blob-storage

我正在尝试使用 azures blob 存储实现以下目标。

  1. 将文件上传到 Azure Blob 存储。
  2. 然后发送包含文件的 base64 字符串的 http 响应。

奇怪的是,我只能让一个工作,因为它会导致另一个停止工作,具体取决于我的代码顺序。

        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 字符串,但是该文件未正确上传到 azure 的 blob 存储。

最佳答案

也许您需要在第一次使用后重置流位置?

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

关于c# - 将文件上传到 azure blob 存储,然后使用文件的 base64 字符串发送 http 响应不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46418526/

相关文章:

c# - 扩展 Label 控件 - 添加 Click 事件处理程序

c# - gridview rowdatabound 事件中的 e.Row.DataItem 错误

.net - 如何实现简单的XPath查找

python - 如何使用 python 列出应用于 azure 资源组的锁

c# - 异步任务运行时如何更改进度条值

c# - 为什么时区偏移返回不同的值?

azure - 启用 EF 代码优先迁移后,Azure 网站上出现内部服务器错误

azure - RoleEnvironment.Changing 事件不会在 WorkerRole 中触发

c# - 是否有可能将 *.pdb 文件包含到发布版本中以查看错误行号?

java - 集成 .net 和 java 应用程序的架构建议