与分段文件写入的问题 [英] A problem with segmented file write

查看:103
本文介绍了与分段文件写入的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下载文件的部分(图片),然后我想这些部分保存到一个文件中。

I download a file's parts (a picture), and then I want to save these parts into one file.

的问题是,第一部分是被下载并正确保存(我可以看到pricture的一部分)。但是,当第二部分被保存(FileMode.Append)图片似乎被打破

The problem is, that the first part is being downloaded and saved properly (I can see the part of that pricture). But, when the second part is saved (FileMode.Append) the picture seems to be broken.

下面的代码:

  HttpWebRequest webRequest;
  HttpWebResponse webResponse;
  Stream responseStream;
  long StartPosition, EndPosition;

        if (File.Exists(LocalPath))
            fileStream = new FileStream(LocalPath, FileMode.Append);
        else fileStream = new FileStream(LocalPath, FileMode.Create);

        webRequest = (HttpWebRequest)WebRequest.Create(FileURL);

        webResponse = (HttpWebResponse)webRequest.GetResponse();
        responseStream = webResponse.GetResponseStream();

        StartPosition = 0;   //download first 52062 bytes of the file
        EndPosition = 52061;

        webRequest.AddRange(StartPosition, EndPosition);

        int SeekPosition = (int)StartPosition;

        while ((bytesSize = responseStream.Read(Buffer, 0, Buffer.Length)) > 0)
        {
            lock (fileStream)
            {
                fileStream.Seek(SeekPosition, SeekOrigin.Begin);
                fileStream.Write(Buffer,0, bytesSize);
            }

  //the Buffer.Length is 2048.
  //When the bytes count to download is < 2048 then I decrease the Buffer.Length
  //to prevent downloading more that 52062 bytes.

            DownloadedBytesCount += bytesSize;
            SeekPosition += bytesSize;

            long TotalToDownload = EndPosition - StartPosition;

            long bytesLeft = TotalToDownload - DownloadedBytesCount;

            if (bytesLeft < Buffer.Length)
                Buffer = new byte[bytesLeft];
        }

当我想下载我设置文件的第二部分。

WHen I want to download the second part of the file I set

    StartPosition = 52062;
    EndPosition = 104122;



再有就是我上述的问题。为什么文件没有正确appened?

and then there is a problem that I described above. Why the file is not appened properly ?

推荐答案

您不需要中StartPosition fileStream.Seek() =缓冲区新的字节[bytesLeft];

另外,锁()不应该是必要的(如果你已经得到了很多更多的麻烦)。

Also the lock() shouldn't be necessary (if it is you've got a lot more troubles).

所以,删除所有,因为机会是你得到了它的一些错误。

So remove all that because the chances are you got some of it wrong.

而如果它当时还不能正常工作,请编辑的问题,并提供更多的信息。有相当多的,现在缺少的:

And if it then still doesn't work, edit the question and provide more information. There is quite a lot missing right now:


  • 您可以使用调试器验证是否在所有执行下载循环。

  • 是如何转换到第二范围52K - 104K执行

  • 在最终生成的文件是多久

  • 确实该文件包含第一个52K字节或第二下载?


  • could you verify with the debugger if the download loop is executed at all.
  • how is the changeover to the 2nd range 52k - 104k performed
  • how long is the resulting file in the end?
  • does the file contain the first 52k bytes or the 2nd download?
  • etc

所有的事项,我们不应该去猜测。

All of that matters and we shouldn't have to guess.

这篇关于与分段文件写入的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆