Windows Azure:存储客户端异常未处理

时间:2010-04-13 06:07:16

标签: c# azure exception azure-blob-storage

我正在编写一个代码,用于使用块将大文件上传到blob中...当我测试它时,它给了我一个StorageClientException

它声明:其中一个请求输入超出范围。

我在这行代码中得到了异常

blob.PutBlock(block, ms, null);

这是我的代码:

protected void ButUploadBlocks_click(object sender, EventArgs e)
        {

            // store upladed file as a blob storage
            if (uplFileUpload.HasFile)
            {
                name = uplFileUpload.FileName;
                byte[] byteArray = uplFileUpload.FileBytes;
                Int64 contentLength = byteArray.Length;
                int numBytesPerBlock = 250 *1024; // 250KB per block
                int blocksCount = (int)Math.Ceiling((double)contentLength / numBytesPerBlock);  // number of blocks 
                MemoryStream ms ;
                List<string>BlockIds = new List<string>();
                string block;
                int offset = 0;

                // get refernce to the cloud blob container
                CloudBlobContainer blobContainer = cloudBlobClient.GetContainerReference("documents");

                // set the name for the uploading files
                string UploadDocName = name;

                // get the blob reference and set the metadata properties
                CloudBlockBlob blob = blobContainer.GetBlockBlobReference(UploadDocName);
                blob.Properties.ContentType = uplFileUpload.PostedFile.ContentType;

                for (int i = 0; i < blocksCount; i++, offset = offset + numBytesPerBlock)
                {
                    block = Convert.ToBase64String(BitConverter.GetBytes(i));
                    ms = new MemoryStream();
                    ms.Write(byteArray, offset, numBytesPerBlock);

                    blob.PutBlock(block, ms, null);
                    BlockIds.Add(block);
                }

                blob.PutBlockList(BlockIds);

                blob.Metadata["FILETYPE"] = "text";
            }
        }

谁能告诉我如何解决它......

1 个答案:

答案 0 :(得分:2)

我认为你必须做ms.Position = 0才能在上传之前将流恢复到开始状态。 (否则,大概PutBlock会尝试从流中读取并在最后找到它。)