asp.net – 将MemoryStream文件存储到Azure Blob
发布时间:2020-12-16 04:22:56 所属栏目:asp.Net 来源:网络整理
导读:我有一个通过System.Drawing动态生成的图像.然后我将生成的图像输出到MemoryStream以存储到我的Azure blob中. 但我似乎无法将我的文件存储在我选择的blob中.没有错误发生,我的图像成功保存到MemoryStream.正如所料,我的blob是空的. 我确保我的blob容器具有公
我有一个通过System.Drawing动态生成的图像.然后我将生成的图像输出到MemoryStream以存储到我的Azure blob中.
但我似乎无法将我的文件存储在我选择的blob中.没有错误发生,我的图像成功保存到MemoryStream.正如所料,我的blob是空的. 我确保我的blob容器具有公共读/写访问权限. 码 // Retrieve storage account from connection string. CloudStorageAccount storageAccount = CloudStorageAccount.Parse(String.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}",Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.GetConfigurationSettingValue("CMSAzureAccountName").ToString(),Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.GetConfigurationSettingValue("CMSAzureSharedKey").ToString())); // Create the blob client. CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); // Retrieve reference to a previously created container. CloudBlobContainer container = blobClient.GetContainerReference("myimagecontainer"); // Retrieve reference to a blob named "myblob". CloudBlockBlob blockBlob = container.GetBlockBlobReference("test.jpg"); //Output image ImageCodecInfo[] Info = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders(); EncoderParameters Params = new System.Drawing.Imaging.EncoderParameters(1); Params.Param[0] = new EncoderParameter(Encoder.Quality,100L); System.IO.MemoryStream msImage = new System.IO.MemoryStream(); GenerateImage.Render(imageOutput).Save(msImage,Info[1],Params); //GenerateImage.Render() method creates a custom image and returns a Bitmap // Create or overwrite the "myblob" blob with contents from a local file. using (var fileStream = msImage) { blockBlob.UploadFromStream(fileStream); } 任何帮助,将不胜感激. 更新 我设法找到了错误的主要原因.我需要更改以下内容: CloudStorageAccount storageAccount = CloudStorageAccount.Parse(String.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}",Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.GetConfigurationSettingValue("CMSAzureSharedKey").ToString())); 至 CloudStorageAccount storageAccount = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("DiagnosticsConnectionString")); 但我会将“Gaurav Mantri”的回应标记为正确.如果不是他的见解,我的图像将不会上传到blob. 解决方法
尝试将内存流的位置设置为0,看看是否有帮助.类似下面的代码:
msImage.Position = 0;//Move the pointer to the start of stream. // Create or overwrite the "myblob" blob with contents from a local file. using (var fileStream = msImage) { blockBlob.UploadFromStream(fileStream); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – coldfusion和.net上的单点登录
- asp.net – .除了抛出异常
- asp.net-mvc – ASP.NET MVC 2中的空查询字符串参数的模型绑
- asp.net-mvc – ASP.Net MVC路由遗传URL将查询字符串ID传递
- asp.net-mvc-4 – Razor MVC,在哪里可以通过母版页面,部分视
- 在ASP.net中使用image404和azurereader2
- asp.net-mvc – 如何单元测试MSTest中的JsonResult和集合
- asp.net – 使用Page.Render覆盖后缓存替换
- asp.net – DataBinding:’System.Data.DataRowView’不包
- 为什么ASP.NET MVC中的移动视图在不同的服务器上显示不同?
推荐文章
站长推荐
- asp.net-mvc – HttpContext.Current调用背后有多
- ASP.NET MVC中的自定义文件夹结构5
- asp.net-mvc – ASP.NET MVC:post-redirect-get
- asp.net – 是否有任何.NET标准用于处理本地化但
- asp.net-mvc-3 – 何时在Mvc3中使用ViewBag,View
- asp.net – 在proc中使用sp_executesql时出现“s
- asp.net – 部署后SQL不会连接
- .net – 为什么IHostedService是异步的
- asp.net-mvc – 在Razor View中重命名mvc模型对象
- asp.net – Automapper ninject依赖项
热点阅读