从memorystream上传到azure存储区返回空文件

时间:2014-12-11 08:09:04

标签: c# asp.net azure azure-storage azure-storage-blobs

我正在使用内存流将System.Drawing.Bitmap编写到Azure存储。我有正确的凭据和一切蔚蓝的一面正确连线。我已经使用输入流成功地将图像上传到blob中,因此我认为这对于我如何使用memorystream对象一定存在问题。

看了一会儿之后,我的问题的一般解决方案看起来是将内存流位置设置为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);
}

最后保存的图像元素中包含数据。在调试时保持正确的长度等,所以问题出现在上传步骤

1 个答案:

答案 0 :(得分:11)

您必须在保存图片后和上传之前回放内存流:

  //...

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

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

  blockBlob.UploadFromStream(ms);
}