locked
How can i simulate files download like offline mode ? RRS feed

  • Question

  • I have a method using WebClient to download files:

    private void fileDownloadRadar()
    {
        if (Client.IsBusy == true)
        {
            Client.CancelAsync();
        }
        else
        {
            Client.DownloadFileAsync(myUri, combinedTemp);
        }
     }

    I also have a checkBox3. If is checked then simulate downloads otherwise don't simulate.

    private void checkBox3_CheckedChanged(object sender, EventArgs e)
    {
        if (checkBox3.Checked)
        {
    
        }
    }


    On my hard disk i have many GIF files that i want to simulate them as downloading files like i'm using the webclient to download them.

    Maybe to create a new method something like:  OfflineFileDownloadRadar()

    And using the WebClient or some other way to simulate downloading the files in my hard disk like they were on a website.

    Is there any way to make this simulation ?

    I thought also maybe to upload some images to my SkyDrive to a new directory i did it already and then just to change the Uri in my program the Uri address to my SkyDrive new directory address and just simulate the downloadings like i'm doing it today only to change th Uri address and the downloading target directory. Is that logic and good idea ?

    In general i want to use the WebClient like today but for testing mode like. And if i uncheck the checkBox3 it will change the Uri address back to the original and will download like a real download mode. Checked = offline mode uncheck = online mode

    I will add some more info in the end when it's finishing downloading a file it's gettintg to the download file completed event:

    private void Client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
            {
    
                if (e.Error != null)
                {
                    timer1.Stop();
                    span = new TimeSpan(0, (int)numericUpDown1.Value, 0);
                    label21.Text = span.ToString(@"mm\:ss");
                    timer3.Start();
                    timer4.Stop();
                }
                else if (!e.Cancelled)
                {
                    timer3.Stop();
                    label19.Visible = false;                
                    bool fileok = Bad_File_Testing(combinedTemp);
                    if (fileok == true)
                    {
                        File1 = new Bitmap(combinedTemp);
                        bool compared = ComparingImages();
                        if (compared == false)
                        {
    
                            DirectoryInfo dir1 = new DirectoryInfo(sf);
                            FileInfo[] fi = dir1.GetFiles("*.gif");
                            last_file = fi[fi.Length - 1].FullName;
                            string lastFileNumber = last_file.Substring(82, 6);
                            int lastNumber = int.Parse(lastFileNumber);
                            lastNumber++;
                            string newFileName = string.Format("radar{0:D6}.gif", lastNumber);
                            identicalFilesComparison = File_Utility.File_Comparison(combinedTemp, last_file);
                            if (identicalFilesComparison == false)
                            {
                                string newfile = Path.Combine(sf, newFileName);
                                File.Copy(combinedTemp, newfile);
                                LastFileIsEmpty();
                            }
                        }
                    }
                    else
                    {
                        File.Delete(combinedTemp);
                    }
                    File1.Dispose();
                }
            }

    In the completed event i also calling a method called: ComparingImages() so only a new image that have been downloaded i will copy and keep it if it's not new i delete it.

    This is the ComparingImages() method:

    private bool ComparingImages()
            {
                for (int x = 0; x < bitmapListToCompare.Count; x++)
                {
                    bl = ImagesComparion1.ImageComparison(File1, bitmapListToCompare[x], image_scan_text_rect, 50);
                    if (bl == true)
                    {
                        File1.Dispose();
                        Properties.Resources.RadarImageClose.Dispose();
                        DrawText(bl, "מכם מזג האוויר סגור");
                        this.Invalidate();
                        break;
                    }
                    else
                    {
                        numberofcomparisons++;
                        if (numberofcomparisons == bitmapListToCompare.Count)
                        {
                            File1.Dispose();
                            Properties.Resources.RadarImageClose.Dispose();
                            DrawText(bl, "מכם מזג האוויר פתוח");
                            this.Invalidate();
                        }
                    }
                }
                if (checkBox2.Checked)
                {
                    CreateAnimatedGif(bl);
                }
                return bl;
            }

    And in this method i'm calling the method i want to test: CreateAnimatedGif(bl);

    private void CreateAnimatedGif(bool ToCreate)
            {
                if (ToCreate == false)
                {
                    AnimatedGifFiles.Add(last_file);
                }
                else
                {
                    for (int i = 0; i < AnimatedGifFiles.Count; i++)
                    {
                        if (!AnimatedGifFiles[i].Contains(last_file))
                            AnimatedGifFiles.Add(last_file);
                    }
                    if (AnimatedGifFiles.Count > 1)
                    {
                        unfreezWrapper1.MakeGIF(AnimatedGifFiles, "dir", 80, true);
                    }
                    AnimatedGifFiles = new List<string>();
                }
            }

    What i'm doing in this method is only if the checkBox3 checked and only if a new file is downloaded it will add each time the new file to the List<string> AnimatedGifFiles.

    Once bl is true it will stop adding to the List and create animated gif file.

    So in general i want to use the webclient like it is today the problem is not that the website niw the Uri now is not working the radar there of weather is not active. So i uploaded images i downloaded already long time ago to a new directory on my SkyDrive and i think to change the Uri address to my SkyDrive so it will think it's the radar the site and that the radar is working.

    Tuesday, October 21, 2014 10:45 PM

Answers

  • The uri for a file is just a string.

    The webclient is just going to try and grab them from whatever uri you give them, it's not calling a web service or something.

    You can therefore hold two collections of uri (string).  Your proper set and your test set.  When you click a button or whatever switch which you will use.

    You could put them anywhere - on a different hard disk or on your onedrive and so long as you give it the right uri then it'll grab the file.

    From example here:

    http://www.c-sharpcorner.com/UploadFile/97fc7a/reading-files-from-given-specific-url-using-webclient/

    He does:

    byte[] myDataBuffer = client.DownloadData((new Uri(strFileUrlToDownload)));
    That is not reliant on any web service, it'll grab it from anywhere you have it.

    So yes, onedrive is a viable option and not a bad idea if you want to test what happens when you have no internet connection.  You can disconnect from the web and check what happens.



    • Edited by Andy ONeill Wednesday, October 22, 2014 8:12 AM
    • Marked as answer by Chocolade1972 Wednesday, October 22, 2014 3:08 PM
    Wednesday, October 22, 2014 8:10 AM

All replies

  • Try hosting a website in local IIS, and place the files there. This way internet will not be required. And without internet you would be able to simulate download of those files using your existing code.
    Wednesday, October 22, 2014 7:18 AM
  • The uri for a file is just a string.

    The webclient is just going to try and grab them from whatever uri you give them, it's not calling a web service or something.

    You can therefore hold two collections of uri (string).  Your proper set and your test set.  When you click a button or whatever switch which you will use.

    You could put them anywhere - on a different hard disk or on your onedrive and so long as you give it the right uri then it'll grab the file.

    From example here:

    http://www.c-sharpcorner.com/UploadFile/97fc7a/reading-files-from-given-specific-url-using-webclient/

    He does:

    byte[] myDataBuffer = client.DownloadData((new Uri(strFileUrlToDownload)));
    That is not reliant on any web service, it'll grab it from anywhere you have it.

    So yes, onedrive is a viable option and not a bad idea if you want to test what happens when you have no internet connection.  You can disconnect from the web and check what happens.



    • Edited by Andy ONeill Wednesday, October 22, 2014 8:12 AM
    • Marked as answer by Chocolade1972 Wednesday, October 22, 2014 3:08 PM
    Wednesday, October 22, 2014 8:10 AM