简体   繁体   中英

How to refresh Memory Stream while downloading from FTP

I have code which downloads file(zip file) from ftp server. But when I change files inside zip file, it is downloading from memory. Telling that I mean when I download zip file in which there are file 1, file 2, file 3. Next time I change zip file inside with file 1, file 2, file 3(but new data in files) and upload it to ftp. When I download from FTP directly using WS_FTP Pro I can see new files. But when I use my code to download zip file I get that file from memory. How can I refresh memory stream so when I download new zip file, I get new files inside zip file.

Mн code is.

public static bool downloadFromWeb(string URL, string file, string targetFolder)
    {
        try
        {
            byte[] downloadedData;
            downloadedData = new byte[0];

            //open a data stream from the supplied URL
            WebRequest webReq = WebRequest.Create(URL + file);
            WebResponse webResponse = webReq.GetResponse();
            Stream dataStream = webResponse.GetResponseStream();

            //Download the data in chuncks
            byte[] dataBuffer = new byte[1024];

            //Get the total size of the download
            int dataLength = (int)webResponse.ContentLength;

            //lets declare our downloaded bytes event args
            ByteArgs byteArgs = new ByteArgs();

            byteArgs.downloaded = 0;
            byteArgs.total = dataLength;

            //we need to test for a null as if an event is not consumed we will get an exception
            if (bytesDownloaded != null) bytesDownloaded(byteArgs);


            //Download the data
            MemoryStream memoryStream = new MemoryStream();
            memoryStream.SetLength(0);
            while (true)
            {
                //Let's try and read the data
                int bytesFromStream = dataStream.Read(dataBuffer, 0, dataBuffer.Length);

                if (bytesFromStream == 0)
                {

                    byteArgs.downloaded = dataLength;
                    byteArgs.total = dataLength;
                    if (bytesDownloaded != null) bytesDownloaded(byteArgs);

                    //Download complete
                    break;
                }
                else
                {
                    //Write the downloaded data
                    memoryStream.Write(dataBuffer, 0, bytesFromStream);

                    byteArgs.downloaded = bytesFromStream;
                    byteArgs.total = dataLength;
                    if (bytesDownloaded != null) bytesDownloaded(byteArgs);
                }
            }

            //Convert the downloaded stream to a byte array
            downloadedData = memoryStream.ToArray();

            //Release resources
            dataStream.Close();
            memoryStream.Close();

            //Write bytes to the specified file
            FileStream newFile = new FileStream(targetFolder + file, FileMode.Create);
            newFile.Write(downloadedData, 0, downloadedData.Length);
            newFile.Close();
            return true;
        }
        catch (Exception)
        {
            //We may not be connected to the internet
            //Or the URL may be incorrect
            return false;
        }
    }

Please indicate me where I should change to download always new zip file from FTP.


I have added couple strings of code in my previous code. It tells browser not to cache. Here is my code with changes.

    public static bool downloadFromWeb(string URL, string file, string targetFolder)
    {
        try
        {
            byte[] downloadedData;
            downloadedData = new byte[0];

            // Set a default policy level for the "http:" and "https" schemes.
            HttpRequestCachePolicy policy = new HttpRequestCachePolicy(HttpRequestCacheLevel.Default);
            HttpWebRequest.DefaultCachePolicy = policy;

            //open a data stream from the supplied URL
            WebRequest webReq = WebRequest.Create(URL + file);

            // Define a cache policy for this request only. 
            HttpRequestCachePolicy noCachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
            webReq.CachePolicy = noCachePolicy;

            WebResponse webResponse = webReq.GetResponse();
            Stream dataStream = webResponse.GetResponseStream();

            //Download the data in chuncks
            byte[] dataBuffer = new byte[1024];

            //Get the total size of the download
            int dataLength = (int)webResponse.ContentLength;

            //lets declare our downloaded bytes event args
            ByteArgs byteArgs = new ByteArgs();

            byteArgs.downloaded = 0;
            byteArgs.total = dataLength;

            //we need to test for a null as if an event is not consumed we will get an exception
            if (bytesDownloaded != null) bytesDownloaded(byteArgs);


            //Download the data
            MemoryStream memoryStream = new MemoryStream();
            memoryStream.SetLength(0);
            while (true)
            {
                //Let's try and read the data
                int bytesFromStream = dataStream.Read(dataBuffer, 0, dataBuffer.Length);

                if (bytesFromStream == 0)
                {

                    byteArgs.downloaded = dataLength;
                    byteArgs.total = dataLength;
                    if (bytesDownloaded != null) bytesDownloaded(byteArgs);

                    //Download complete
                    break;
                }
                else
                {
                    //Write the downloaded data
                    memoryStream.Write(dataBuffer, 0, bytesFromStream);

                    byteArgs.downloaded = bytesFromStream;
                    byteArgs.total = dataLength;
                    if (bytesDownloaded != null) bytesDownloaded(byteArgs);
                }
            }

            //Convert the downloaded stream to a byte array
            downloadedData = memoryStream.ToArray();

            //Release resources
            dataStream.Close();

            memoryStream.Close();

            //Write bytes to the specified file
            FileStream newFile = new FileStream(targetFolder + file, FileMode.Create);
            newFile.Write(downloadedData, 0, downloadedData.Length);
            newFile.Close();
            return true;
        }
        catch (Exception)
        {
            //We may not be connected to the internet
            //Or the URL may be incorrect
            return false;
        }
    }

I believe your get-request is being cached by the WebRequest. Try adding a random query parameter and see if it helps.

WebRequest webReq = WebRequest.Create(URL + file + "?nocache=" + DateTime.Now.Ticks.ToString());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM