root/trunk/nuxleus/Source/Xameleon/Function/FileStream.cs

Revision 4418, 2.7 kB (checked in by xmlhacker, 1 year ago)

--

Line 
1 using System;
2 using System.Diagnostics;
3 using System.Drawing;
4 using System.IO;
5 using System.Net;
6 using System.Web;
7 using Nuxleus.Cryptography;
8 using System.Collections;
9 using System.Text;
10
11 namespace Xameleon.Function
12 {
13     public class HttpFileStream
14     {
15
16         public static string SaveUploadedFileCollection (HttpRequest request, string fieldName, string fileName)
17         {
18            
19             IEnumerator enumerator = request.Files.GetEnumerator();
20             StringBuilder filePathStringBuilder = new StringBuilder();
21             string path = request.MapPath(fileName);
22
23             try
24             {
25                 if (!Directory.Exists(path))
26                 {
27                     DirectoryInfo directory = Directory.CreateDirectory(path);
28                 }
29
30                 for (int i = 0; enumerator.MoveNext(); i++)
31                 {
32                     string hash = new HashcodeGenerator(request.Files[i].InputStream).GetHashCode().ToString();
33                     string filePath = String.Format("{0}/{1}{2}", path, hash, Path.GetExtension(request.Files[i].FileName));
34                     request.Files[i].SaveAs(filePath);
35                     filePathStringBuilder.Append(String.Format("{0}:{1},", Path.GetFileName(filePath), request.Files[fieldName].ContentType));
36                 }
37
38                 return filePathStringBuilder.ToString();
39             }
40             catch (Exception e)
41             {
42                 return e.Message;
43             }
44
45         }
46
47         public static void SaveExternalImageFile (string externalFile, string fileName)
48         {
49             try
50             {
51                 using (Stream stream = GetFileStream(externalFile))
52                 {
53                     if (stream != null)
54                     {
55                         Image image = Image.FromStream(stream);
56                         image.Save(fileName, image.RawFormat);
57                     }
58                 }
59             }
60             catch (Exception e)
61             {
62                 Debug.WriteLine("Error: " + e.Message);
63             }
64         }
65
66         private static Stream GetFileStream (string fileURL)
67         {
68
69             try
70             {
71                 WebRequest fileRequest = WebRequest.Create(fileURL);
72                 fileRequest.Timeout = 5000;
73                 ((HttpWebRequest)fileRequest).UserAgent = "XameleonWebCrawler/1.0 (compatible; http://xameleon.org/)";
74
75                 WebResponse fileResponse = fileRequest.GetResponse();
76                 return fileResponse.GetResponseStream();
77
78             }
79             catch (Exception e)
80             {
81                 Debug.WriteLine(e.Message);
82                 return null;
83             }
84         }
85     }
86 }
87
88
Note: See TracBrowser for help on using the browser.