0

Need Your Help. When i downloaded myself it opening but when i downloading with c# windows form it Downloading Successfully but not opening, am using this code to download Rar or Zip File.

private void downloadBtn_Click(object sender, EventArgs e) 
{
    WebClient client = new WebClient();
    string tAddress = "Download Link"; // When i Downloaded mySelf it Worked And Opened
    string fileName = "Testfile.Zip";
    Uri uri = new Uri(tAddress);
    client.DownloadFileCompleted += Client_DownloadFileCompleted;
    client.DownloadFileAsync(uri, fileName);

}
private void Client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
    MessageBox.Show("Download Completed Successfully!");
}

This is Error When I Trying To Open Rar Or Zip File. It Says Error, This File Has Incorrect Format Or Damaged, But When I Try To Download Myself Its Opened

GuKkA
  • 3
  • 2
  • How does the size of the file compare when you download it yourself vs when downloaded with this code? – Code Stranger Jun 15 '20 at 19:31
  • how are you opening the file and where is it completed i do not see any `await` are you simply missing `await client.DownloadFileAsync(uri, fileName);` otherwise change to `client.DownloadFile(uri, fileName);` – Seabizkit Jun 15 '20 at 19:33
  • @Code Stranger when i downloaded myself size is 3MB and with code is 150kb, i dont know whats happening\ – GuKkA Jun 15 '20 at 19:34
  • @Seabizkit when i trying `await client.DownloadFileAsync(uri, fileName)` or `await client.DownloadFile(uri, fileName)` it brings me error, can you give me code how to do it? – GuKkA Jun 15 '20 at 19:40
  • @GuKkA to confrim it works it would be just `client.DownloadFile(uri, fileName)` not `await client.DownloadFile(uri, fileName)` the key word `await` is used on Async methods... so try just `client.DownloadFile(uri, fileName)` if that works... then you can research `Async` – Seabizkit Jun 16 '20 at 07:04
  • @Seabizkit i Tryed That But Not Working I Tryed Many Method But Still Not Working – GuKkA Jun 16 '20 at 08:33
  • include the name of the target file, is it .zip or .rar – Seabizkit Jun 16 '20 at 08:53
  • @Seabizkit https://workupload.com/start/c2kh9NB This Is Link Its A Test.Zip File – GuKkA Jun 16 '20 at 09:55
  • show what you are putting for the url – Seabizkit Jun 16 '20 at 12:06
  • @Seabizkit `string tAddress = "https://workupload.com/start/c2kh9NB";` – GuKkA Jun 16 '20 at 14:54
  • i think ur url is wrong, i think u want https://alioth.workupload.com/download/c2kh9NB, that url is the page load... tricky...its not actually the file itself.... – Seabizkit Jun 16 '20 at 21:12
  • @Seabizkit can you give me that test.zip currect download link? and i'l Try it – GuKkA Jun 17 '20 at 10:23
  • u not making sense i gave u the url.... check in the download tab in chome.... what the actually url is.... which is the one i gave...."https://workupload.com/start/c2kh9NB" is a link to a page resource not a file resource.. it just looks like its the file as to what the page is doing... see below i see they doing tricky – Seabizkit Jun 17 '20 at 11:20
  • https://caph.workupload.com/download/c2kh9NB – Seabizkit Jun 17 '20 at 11:22
  • clicking the link in a browser should just download the file... not load a page.. – Seabizkit Jun 17 '20 at 11:23
  • at the time of writing this link caph.workupload.com/download/c2kh9NB link is valid, but my guess is they may generate this on the fly... to prevent simply consumption. as alioth.workupload.com/download/c2kh9NB was valid, but now load the page instead of file. so i think ur problem is ur tring to consume a 3rd party which has measure in place to prevent you from just downloading in a simply way... this is probably protection from miss-use which they don't want. – Seabizkit Jun 17 '20 at 11:53
  • @Seabizkit Yea Its Downloading When I Click Link, I Pasted It In tAddress but Still Not Opening – GuKkA Jun 17 '20 at 12:11
  • create console version i can copy and paste and check what u saying.. – Seabizkit Jun 17 '20 at 13:38
  • my test i cant get the actual file im just hittin the page, my file size is 12kb, which isnt right are you gettin the right file size? – Seabizkit Jun 17 '20 at 13:54
  • try this... yeah looks like what i was saying the site is making it hard....https://stackoverflow.com/questions/60307832/why-doesnt-webrequest-allowautoredirect-redirect-the-initial-url-to-the-correct – Seabizkit Jun 17 '20 at 13:59
  • Are the files identical? Can you compare both files with an md5 hash? or a file compare tool? – dariogriffo Jun 17 '20 at 14:24
  • @Seabizkit My File Size is 12kb too – GuKkA Jun 17 '20 at 15:25
  • @GuKkA see answer i posted... – Seabizkit Jun 17 '20 at 15:34

2 Answers2

0

Use a try catch block to spot the exception it throws:

try
{
    WebClient client = new WebClient();
        string tAddress = "Download Link"; // When i Downloaded mySelf it Worked And Opened
        string fileName = "Testfile.Zip";
        Uri uri = new Uri(tAddress);
        client.DownloadFileCompleted += Client_DownloadFileCompleted;
        client.DownloadFileAsync(uri, fileName);
}
catch(Excepction ex)
{
    MessageBox.Show(ex.Message)
}

Its possible that the problem is in Client.DownloadFileAsync() in that case use the method : "ZipFile.Open" Also, for rar try this: https://stackoverflow.com/a/11523864/12764752

0

the site is not being friendly maybe on purpose who knows but

if you follow what hes doing here it will work..

Why doesn't WebRequest.AllowAutoRedirect redirect the initial url to the correct download url?

Seabizkit
  • 2,417
  • 2
  • 15
  • 32