c# - 从内存流上传到 azure 存储返回空文件

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

我正在使用内存流将 System.Drawing.Bitmap 写入 Azure 存储。我有正确的凭据,并且 azure 的所有内容都正确连接。我已使用输入流成功将图像上传到 blob,因此我认为这一定是我使用内存流对象的方式存在问题。

经过一段时间的研究,我的问题的一般解决方案似乎是将内存流位置设置为 0,但这对我来说不起作用,并且空文件仍然被保存到 azure 中。

我的代码是:

 using (image)
 {
 System.IO.MemoryStream ms = new MemoryStream();

 //create an encoder parameter for the image quality
 EncoderParameter qualityParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);

 //get the jpeg codec
 ImageCodecInfo imgCodec = ImageUtilities.GetEncoderInfo(CodecInfo);

  //create a collection of all parameters that we will pass to the encoder
  EncoderParameters encoderParams = new EncoderParameters(1);

  //set the quality parameter for the codec
  encoderParams.Param[0] = qualityParam;

  //Move the pointer to the start of stream.
  ms.Position = 0;

  image.Save(ms, imgCodec, encoderParams);

  blockBlob.UploadFromStream(ms);
}

最后保存的图像元素中有数据。在调试时保持正确的长度等,因此问题出在上传步骤中的某个地方

最佳答案

保存图像后和上传之前,您必须倒回内存流:

  //...

  //Move the pointer to the start of stream.
  ms.Position = 0;

  image.Save(ms, imgCodec, encoderParams);
  //HERE !!!
  ms.Position = 0;

  blockBlob.UploadFromStream(ms);
}

关于c# - 从内存流上传到 azure 存储返回空文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27418053/

相关文章:

c# - 将程序集作为模块/插件加载,同时避免重复和脆弱性

c# - Object cannot be cast from DBNull to other types 错误显示?

c# - 基于对象集合的动态ContextMenu的模式/方法

c# - 使用 Microsoft Translator 的客户端没有声音

json - 如何使用等效的 SELECT DISTINCT 语句查询 Azure DocumentDB

c# - Azure 函数 - 在新 blob 上设置 CloudBlockBlob 元数据

c# - 异步 ICommand 实现

jquery - ASP.NET List<> 与 AJAX AutoComplete For JQuery 一起使用。 ?要么转换为 JS JSON 要么 ajax 调用

asp.net - 如何使用 ASP.NET 读取和写入 Amazon Elasticache

azure - 如何在 Terraform for LogicApps 中创建 if 语句