开发者

A problem with segmented file write

开发者 https://www.devze.com 2023-01-15 15:25 出处:网络
I download a file\'s parts (a picture), and then I want to save these parts into one file. The problem is, that the first part is being downloaded and saved properly (I can see the part of that prict

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

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.

Here's the code:

  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;

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


You don't need StartPosition, fileStream.Seek() and Buffer = new byte[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:

  • 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.


What i would try is to download the image some way that you know that it works and compare the byte result to check where the file gets broken and what is breaking it...


This code is wicked... sorry but you must start by deleting all the code and looking at your problem from the beginning. There are many better ways to accomplish what you want. Just take a look at some good solutions:
http://www.codeproject.com/KB/IP/MyDownloader.aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消