2010-04-13 126 views
1

我使用块写上传大文件代码到斑点......当我测试了它,它给了我一个StorageClientException的Windows Azure:存储客户端异常未处理

它指出:其中一个请求输入是超出范围。

我得到这个例外在这行代码:

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"; 
      } 
     } 

谁能告诉我该怎么解决呢?

+0

您是否在首次调用PutBlock时或者在上传几个块后得到异常?像,块0-9上传正常,但块10给出了错误? – dthorpe 2010-04-13 06:27:13

回答

2

我认为你必须做ms.Position = 0才能在上传之前让流回到开始。 (否则,大概PutBlock会尝试从流中读取数据,并在最后发现它。)