下载文件 - 如果存在于服务器中 - c# [英] Download a File - If exists in server - c#

查看:64
本文介绍了下载文件 - 如果存在于服务器中 - c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个代码,

Hi ,
I have a code ,

WebClient webClient = new WebClient();
webClient.DownloadFileAsync(new Uri("http://MySite.com/Desktop/Pics.png"), @"c:\users\Windows\desktop\DesktopsPics\Desktop.png");





我的程序将每天下载一张.png图片作为文件夹中的Daily Pics!

所以,当用户点击按钮时,如果服务器中已存在每日图片,程序会下载此文件...

我可以用上面的代码执行此操作,但是,如果服务器中尚未存在Pic.Png,我的程序会出错!!!! (它下载了一个找不到404的.html文件:)





所以如何下载文件,如果这个文件存在于服务器中?!?:D



谢谢



My Program will download a .png picture every day as "Daily Pics" in a folder!
so , when user click on a button, if "Daily Pic" is already exists in server ,program download this file ...
I can do this with above code , but , if Pic.Png is not already exists in server , my program work with error !!!! (it download a .html file that is 404 not found" :)


so how can i download a file , if this file exist in a server ?!? :D

Thanks

推荐答案

你可以查看文件是否排除在您的网络服务器上。



You can chrck if a file exitst on your web server.

// create the request
HttpWebRequest request = WebRequest.Create(url);;

// instruct the server to return headers only
request.Method = "HEAD";

// make the connection
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

// get the status code
HttpStatusCode status = response.StatusCode;





现在你可以检查列出的响应代码这里



HTTP_STATUS_NOT_FOUND 正是您要找的。



Source [ ^ ]



Now you can check the responce codes against the ones listed here

HTTP_STATUS_NOT_FOUND is what you are looking for.

Source[^]


这篇关于下载文件 - 如果存在于服务器中 - c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆