简体   繁体   English

如何在 C# 中使用同步 webclient 请求下载文件而不显示弹出窗口

[英]how to download a file using sync webclient request in c# with out showing popup

I am using "webclient" to download and save a file by url in windows application.我正在使用“webclient”通过 Windows 应用程序中的 url 下载和保存文件。

here is my code:这是我的代码:

WebClient wc = new WebClient();
wc.Headers.Add(HttpRequestHeader.Cookie, cc);
wc.DownloadFile(new Uri(e.Url.ToString()), targetPath);

this is working fine local system.(downloading the file and saved to target path automatically with out showing any popup).这在本地系统中工作正常。(下载文件并自动保存到目标路径而不显示任何弹出窗口)。 But when i am trying to execute the .exe in server its showing save/open popup.但是当我尝试在服务器中执行 .exe 时,它​​会显示保存/打开弹出窗口。 Is there any modifications require to download a file in server settings.在服务器设置中下载文件是否需要任何修改。 Please help me to download the file with out showing popup in server too.请帮我下载文件而不在服务器中显示弹出窗口。

thanks in advance..提前致谢.. 在此处输入图片说明

Finally i got the solution for this issue.. herw the code: 最后我得到了这个问题的解决方案.. herw代码:

WebClient wc = new WebClient();
wc.Headers.Add(HttpRequestHeader.Cookie, cc);
using (Stream data = wc.OpenRead(new Uri(e.Url.ToString())))
{
    using (Stream targetfile = File.Create(targetPath))
    {
       data.CopyTo(targetfile);
    }
}

here i just replaced the code 在这里我只是替换了代码

wc.DownloadFile(new Uri(e.Url.ToString()), targetPath);

with the blow lines: 用吹线:

using (Stream data = wc.OpenRead(new Uri(e.Url.ToString())))
{
 using (Stream targetfile = File.Create(targetPath))
 {
    data.CopyTo(targetfile);
 }
}

Now its working fine.. Thanks all for ur response.. 现在它工作正常..谢谢大家的回复..

By default you need administrative permissions to save to targetPath if it lies on protected folder like the ProgramFiles and Windows Folder. 默认情况下,如果它位于受保护的文件夹(如ProgramFiles和Windows文件夹),则需要管理权限才能保存到targetPath。 so you have two choices: 所以你有两个选择:

  1. Run the executable as administrator on server 在服务器上以管理员身份运行可执
  2. ensure that targetPath is a location like the temp folder or appData folder. 确保targetPath是临时文件夹或appData文件夹的位置。

  3. Probably a possible duplicate of Can't download files from the computer with enabled TLS 1.1/1.2 protocols using WebClient.DownloadFile method 可能是一个可能的副本无法使用WebClient.DownloadFile方法从启用了TLS 1.1 / 1.2协议的计算机上下载文件

hope that helps! 希望有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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