2017-03-06 86 views
1

我從下面的代碼中收到錯誤。 錯誤416請求的範圍不滿足在我的自定義下載文件方法。 我的文件是一個帶有兩個pdf的zip文件。此代碼僅針對特定文件。我有55個文件,其中只有一個文件給我這個錯誤。文件正在從Azure網站目錄上載/下載。錯誤416請求的範圍不滿足在c#

見下面該文件的屬性窗口:

enter image description here

這是我的代碼:

try 
{ 
    var packageId = updates[0]; 
    var packagePath = updates[1]; 
    var packageNameAvailable = Path.GetFileName(updates[1]); 
    log.Info($"Package id {packageId} | {packageNameAvailable} is available to download. "); 
    DownloadPackagePath = string.Format(@"{0}\{1}", ApiConfigHelper.PackageRootDirectory, packageNameAvailable); 
    var url = new Uri(updates[1]); 
    **DownloadFile(url.OriginalString, DownloadPackagePath);** // problem here 
    result = true; 
} 
catch (Exception ex) 
{ 
    log.Info("Error occurred while downloading package, stopping download. Cleaning up resources. "); 
    log.Error($"Error:{ex.Message}", ex); 
    log.Info("Cleaning up started...."); 
    result = false; 
} 

DownloadFile方法:

private void DownloadFile(string sourceURL, string destinationPath) 
    { 
     long fileSize = 0; 
     int bufferSize = 1024; 
     bufferSize *= 1000; 
     long existLen = 0; 
     FileStream saveFileStream = null; 
     Stream resStream = null; 
     try 
     { 
      log.Info("Download started...."); 

      if (File.Exists(destinationPath)) 
      { 
       FileInfo destinationFileInfo = new FileInfo(destinationPath); 
       existLen = destinationFileInfo.Length; 
       log.Info($"Resuming partial downloaded file from {existLen/1024} kB started...."); 
      } 

      if (existLen > 0) 
      { 
       saveFileStream = new FileStream(destinationPath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite); 
      } 
      else 
      { 
       saveFileStream = new FileStream(destinationPath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite); 
       FileInfo destinationFileInfo = new FileInfo(destinationPath); 
       log.Info($"Starting download FileName:{destinationFileInfo.Name} Size: {destinationFileInfo.Length/1024} kB ...."); 
      } 

      var httpRequest = (HttpWebRequest)WebRequest.Create(sourceURL); 
      httpRequest.AddRange((int)existLen); 
      var httpResponse = (HttpWebResponse)httpRequest.GetResponse(); 
      resStream = httpResponse.GetResponseStream(); 
      fileSize = httpResponse.ContentLength; 
      int byteSize; 
      byte[] downBuffer = new byte[bufferSize]; 

      while ((byteSize = resStream.Read(downBuffer, 0, downBuffer.Length)) > 0) 
      { 
       saveFileStream.Write(downBuffer, 0, byteSize); 
      } 
      log.Info("File downloaded successfully. Clean up started...."); 

     } 
     catch 
     { 
      throw; 

     } 
     finally 
     { 
      log.Info("Cleaning up unused streams...."); 
      if (saveFileStream != null) 
      { 
       saveFileStream.Close(); 
       saveFileStream.Dispose(); 

      } 
      if (resStream != null) 
      { 
       resStream.Close(); 
       resStream.Dispose(); 
      } 
      log.Info("DONE!!!"); 

     } 
    } 

能否請您幫助我認同克this.My日誌有一個條目說 恢復部分下載的文件從2494 kB開始....並堅持只有。

回答

0

從2494恢復部分下載的文件,根據日誌信息KB開始

,請求範圍爲2494 * 1024(2553856)啓動。 2,553,856字節大於可從文件屬性窗口中看到的文件大小(2,492,548)。您請求的文件範圍不存在,將導致416(請求範圍不可滿足)錯誤。

原因可能是存在一個存在的文件。它與您要下載的zip文件具有相同的名稱。嘗試刪除同一文件夾中的存在文件將解決此問題。