错误416 c#中请求的范围不满足

时间:2017-03-06 15:14:35

标签: c# visual-studio http azure-web-sites

我从下面的代码中收到错误。我的自定义 DownloadFile 方法中的错误416请求的范围不满意。 我的文件是一个包含两个pdf的zip文件。此代码仅针对该特定文件。我有55个文件,其中只有一个文件给我这个错误。文件正在上传/下载到Azure网站目录。

请参阅该文件下面的属性窗口:

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!!!");

        }
    }

请你帮我识别一下。我的日志有一个条目说 恢复部分下载文件从2494 kB开始.... 并坚持只有。

1 个答案:

答案 0 :(得分:0)

  

从2494 kB开始恢复部分下载文件

根据日志信息,请求范围从2494 * 1024(2553856)开始。 2,553,856字节大于文件大小(2,492,548),可以从文件属性窗口中看到。您请求的文件范围不存在将导致416(请求范围不可满足)错误。

原因可能是存在文件。它与您要下载的zip文件同名。尝试删除同一文件夹中的现有文件将解决此问题。

相关问题