2011-12-29 105 views
5

我試圖直接將文件上傳到azure blob存儲時出現問題。我正在使用ajax調用來發送post請求到一個ashx處理程序來上傳一個blob塊。我遇到的問題是處理程序沒有收到從ajax post發送的filechunk。上傳到Azure

我可以看到的頁面是從看着螢火蟲的要求正確地接收到後,

---------------------- ------- 265001916915724內容處理:表單數據; >名稱= 「切片」;文件名=「斑點」內容類型:應用/八位字節流

我注意到在處理程序中的輸入流具有filechunk,包括從該請求的附加字節。我嘗試從inputstream中只讀取filechunk的大小,但是這導致了一個損壞的文件。

我從http://code.msdn.microsoft.com/windowsazure/Silverlight-Azure-Blob-3b773e26得到靈感,我簡單地將它從MVC3轉換爲使用標準aspx。

下面是使用AJAX的文件塊發送到aspx頁面調用,

var sendFile = function (blockLength) { 
var start = 0, 
    end = Math.min(blockLength, uploader.file.size), 
    incrimentalIdentifier = 1, 
    retryCount = 0, 
    sendNextChunk, fileChunk; 
uploader.displayStatusMessage(); 
sendNextChunk = function() { 
    fileChunk = new FormData(); 
    uploader.renderProgress(incrimentalIdentifier); 
    if (uploader.file.slice) { 
     fileChunk.append('Slice', uploader.file.slice(start, end)); 
    } 
    else if (uploader.file.webkitSlice) { 
     fileChunk.append('Slice', uploader.file.webkitSlice(start, end)); 
    } 
    else if (uploader.file.mozSlice) { 
     fileChunk.append('Slice', uploader.file.mozSlice(start, end)); 
    } 
    else { 
     uploader.displayLabel(operationType.UNSUPPORTED_BROWSER); 
     return; 
    } 
    var testcode = 'http://localhost:56307/handler1.ashx?create=0&blockid=' + incrimentalIdentifier + '&filename=' + uploader.file.name + '&totalBlocks=' + uploader.totalBlocks; 
    jqxhr = $.ajax({ 
     async: true,   
     url: testcode, 
     data: fileChunk, 
     contentType: false, 
     processData:false, 
     dataType: 'text json',   
     type: 'POST', 
     error: function (request, error) { 
      if (error !== 'abort' && retryCount < maxRetries) { 
       ++retryCount; 
       setTimeout(sendNextChunk, retryAfterSeconds * 1000); 
      } 

      if (error === 'abort') { 
       uploader.displayLabel(operationType.CANCELLED); 
       uploader.resetControls(); 
       uploader = null; 
      } 
      else { 
       if (retryCount === maxRetries) { 
        uploader.uploadError(request.responseText); 
        uploader.resetControls(); 
        uploader = null; 
       } 
       else { 
        uploader.displayLabel(operationType.RESUME_UPLOAD); 
       } 
      } 

      return; 
     }, 
     success: function (notice) { 
      if (notice.error || notice.isLastBlock) { 
       uploader.renderProgress(uploader.totalBlocks + 1); 
       uploader.displayStatusMessage(notice.message); 
       uploader.resetControls(); 
       uploader = null; 
       return; 
      } 

      ++incrimentalIdentifier; 
      start = (incrimentalIdentifier - 1) * blockLength; 
      end = Math.min(incrimentalIdentifier * blockLength, uploader.file.size); 
      retryCount = 0; 
      sendNextChunk(); 
     } 
    }); 
}; 

感謝這麼多的東西,可以幫助我。

回答

0

發現在我的webform上,輸入文件標記缺少enctype =「multipart/form-data」屬性。

0

這是ASPX的目的?在http://localhost:56307/handler1ashx?create = 0 & blockid?

+0

不是故意的,我試過使用泛型處理程序而不是aspx頁面,但這兩個選項仍然無法讀取正在發送的文件chunk,Request.Files仍爲空。 – Zephon 2012-01-03 04:26:25