2010-04-13 56 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會嘗試從流中讀取數據,並在最後發現它。)