Geeks With Blogs
Łukasz Kuryło's blog
  1. Download a remote file using ASP.NET
  1. Download a remote file using ASP.NET
  1:          public bool DownloadFile(string url, string downloadFileName, 
2: string directoryToSave, out Exception exception)
3: {
4: string fileName = downloadFileName;
5: bool result = false;
6: exception = null;
7:
8: if(String.IsNullOrEmpty(url))
9: {
10: throw new ArgumentNullException("url");
11: }
12:
13: if (!String.IsNullOrEmpty(directoryToSave))
14: {
15: if (!Directory.Exists(Server.MapPath(directoryToSave)))
16: {
17: throw new ArgumentException(
18: String.Format("The directory {0} doesn't exist", directoryToSave));
19: }
20: }
21:
22: if (String.IsNullOrEmpty(fileName))
23: {
24: int index = url.LastIndexOf('/');
25: fileName = url.Substring(index + 1);
26: }
27:
28: fileName = String.IsNullOrEmpty(directoryToSave) ?
29: Server.MapPath("Download/" + fileName) :
30: Server.MapPath(directoryToSave +'/'+ fileName);
31:
32: try
33:
{
34: System.Net.WebClient client = new System.Net.WebClient();
35: client.DownloadFile(url, fileName);
36: result = true;
37: }
38: catch (Exception ex)
39: {
40: exception = ex;
41: }
42:
43: return result;
44: }

Posted on Monday, October 5, 2009 10:06 PM ASP.NET MVC , useful snippets , ASP.NET | Back to top


Comments on this post: asp.net /asp.net mvc snippets

Comments are closed.
Comments have been closed on this topic.
Copyright © Łukasz Kuryło | Powered by: GeeksWithBlogs.net