Note that there are some explanatory texts on larger screens.

plurals
  1. POSave zip file from url C#
    primarykey
    data
    text
    <p>I'm trying to save a zip file from a URL that doesn't directly link to the file (like <a href="http://website.com/DocMgm/DocMgmServlet?docNum=pce-0011&amp;attachmentType=Archive" rel="nofollow">http://website.com/DocMgm/DocMgmServlet?docNum=pce-0011&amp;attachmentType=Archive</a>). When I use the link in the browser it brings up the download file option but I can't seem to get it to work programatically.</p> <p>When I try to do either method the created zip file produces the following error:</p> <blockquote> <p>Can not open file 'C:\temp\PCE-0011.zip' as archive</p> </blockquote> <p>The url is fake. I don't know the exact relationship between that and the file on the server, I think it works as a redirect to the most current version of the file (it is through a document control system).</p> <p>Here are the two general ways I've tried:</p> <pre><code> string ZipURL = "http://website.com/DocMgm/DocMgmServlet?docNum=pce-0011&amp;attachmentType=Archive"; string SaveURL = "c:\\temp\\PCE-0011.zip"; WebClient webClient = new WebClient(); webClient.DownloadFile(ZipURL, @SaveURL); </code></pre> <p>And:</p> <pre><code> string url = "http://website.com/DocMgm/DocMgmServlet?docNum=pce-0011&amp;attachmentType=Archive"; WebRequest objRequest = System.Net.HttpWebRequest.Create(url); WebResponse objResponse = objRequest.GetResponse(); byte[] buffer = new byte[32768]; using (Stream input = objResponse.GetResponseStream()) { using (FileStream output = new FileStream("C:\\temp\\PCE-0011.zip", FileMode.CreateNew)) { int bytesRead; while ((bytesRead = input.Read(buffer, 0, buffer.Length)) &gt; 0) { output.Write(buffer, 0, bytesRead); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload