1: using System;
2: using System.IO;
3: using System.Web;
4:
5: namespace Ia.Cl.Model
6: {
7: ////////////////////////////////////////////////////////////////////////////
8:
9: /// <summary publish="true">
10: /// Handle file uploading functions.
11: /// </summary>
12: /// <value>
13: /// Make sure you set permissions for IUSR and IWP to Read and Write for upload directory.
14: /// </value>
15: /// <remarks>
16: /// Copyright � 2001-2016 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated Applications - Kuwait. All Rights Reserved.
17: ///
18: /// This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
19: /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
20: ///
21: /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
22: /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
23: ///
24: /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
25: ///
26: /// Copyright notice: This notice may not be removed or altered from any source distribution.
27: /// </remarks>
28: public class Upload
29: {
30: ////////////////////////////////////////////////////////////////////////////
31:
32: /// <summary>
33: ///
34: /// </summary>
35: public Upload() { }
36:
37: ////////////////////////////////////////////////////////////////////////////
38:
39: /// <summary>
40: ///
41: /// </summary>
42: public static string FileName
43: {
44: get
45: {
46: HttpFileCollection hfc = HttpContext.Current.Request.Files;
47: HttpPostedFile hpf = hfc[0];
48:
49: return hpf.FileName;
50: }
51: }
52:
53: ////////////////////////////////////////////////////////////////////////////
54:
55: /// <summary>
56: ///
57: /// </summary>
58: public static bool File(string pathFile)
59: {
60: return File(pathFile, true);
61: }
62:
63: ////////////////////////////////////////////////////////////////////////////
64:
65: /// <summary>
66: ///
67: /// </summary>
68: public static bool File(string absoluteOrRelativePathFile, bool useTemporaryFolder)
69: {
70: bool b;
71: string absolutePath, directory;
72:
73: b = false;
74:
75: HttpFileCollection hfc = HttpContext.Current.Request.Files;
76: HttpPostedFile hpf = hfc[0];
77:
78: /*
79:
80: #if WFA
81: absolute_path = AppDomain.CurrentDomain.BaseDirectory.ToString() + @"app_data\temp\";
82: #else
83: if (hpf.ContentLength > 0)
84: {
85: if (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed) absolute_path = System.Deployment.Application.ApplicationDeployment.CurrentDeployment.DataDirectory + @"\";
86: else absolute_path = AppDomain.CurrentDomain.BaseDirectory;
87:
88:
89: hpf.SaveAs(absolute_path + relative_path_file);
90: b = true;
91: }
92: #endif
93: */
94:
95: try
96: {
97: // this will detect the initial "C:\" in the path_file to decide if it is absolute or relative
98: if (absoluteOrRelativePathFile.Contains(@":\"))
99: {
100: // absolute path file
101: absolutePath = absoluteOrRelativePathFile;
102: }
103: else
104: {
105: absolutePath = Ia.Cl.Model.Default.AbsolutePath(useTemporaryFolder);
106:
107: absolutePath += absoluteOrRelativePathFile;
108: }
109:
110: directory = absolutePath.Substring(0, absolutePath.LastIndexOf(@"\"));
111:
112: if (!Directory.Exists(directory))
113: {
114: // create the directory it does not exist.
115: Directory.CreateDirectory(directory);
116: }
117:
118: hpf.SaveAs(absolutePath);
119: b = true;
120:
121: /*
122: using (FileStream fs = new FileStream(absolute_path, FileMode.Create))
123: {
124: using (StreamWriter sw = new StreamWriter(fs, Encoding.UTF8))
125: {
126: sw.WriteLine(line);
127: b = true;
128: }
129: }
130: */
131: }
132: catch (Exception)
133: {
134: b = false;
135: }
136:
137: return b;
138: }
139:
140: ////////////////////////////////////////////////////////////////////////////
141:
142: /// <summary>
143: /// Read and file into a stream
144: /// </summary>
145: public static StreamReader Stream()
146: {
147: StreamReader sr;
148: HttpFileCollection hfc = HttpContext.Current.Request.Files;
149: HttpPostedFile hpf = hfc[0];
150:
151: sr = null;
152:
153: try
154: {
155: sr = new StreamReader(hpf.InputStream);
156: }
157: catch (Exception)
158: {
159: }
160: finally
161: {
162: }
163:
164: return sr;
165: }
166:
167: ////////////////////////////////////////////////////////////////////////////
168:
169: /// <summary>
170: ///
171: /// </summary>
172: public static bool File(string path, int number)
173: {
174: return File(path, number, true);
175: }
176:
177: ////////////////////////////////////////////////////////////////////////////
178:
179: /// <summary>
180: ///
181: /// </summary>
182: public static bool File(string file, int number, bool tempFolder)
183: {
184: bool b;
185: int i, n, l;
186: string path, pathFile/*, ext*/;
187:
188: b = false;
189: n = 0;
190: //path = null;
191:
192: HttpFileCollection hfc = HttpContext.Current.Request.Files;
193:
194: for (i = 0; i < hfc.Count; i++)
195: {
196: pathFile = "";
197:
198: HttpPostedFile hpf = hfc[i];
199:
200: l = hpf.FileName.LastIndexOf('.') + 1;
201:
202: //ext = hpf.FileName.Substring(l, hpf.FileName.Length - l);
203:
204: try
205: {
206: if (hpf.ContentLength > 0)
207: {
208: if (tempFolder)
209: {
210: path = Ia.Cl.Model.Default.AbsolutePath(tempFolder);
211:
212: pathFile = path + file + "_" + number + "_" + n;// + "." + ext;
213: }
214: else
215: {
216: path = Ia.Cl.Model.Default.AbsolutePath();
217:
218: pathFile = path + file + "_" + number + "_" + n;// +"." + ext;
219: }
220:
221: hpf.SaveAs(pathFile);
222: b = true;
223: n++;
224: }
225: }
226: catch (Exception)
227: {
228: b = false;
229: }
230: }
231:
232: return b;
233: }
234:
235: ////////////////////////////////////////////////////////////////////////////
236:
237: /// <summary>
238: ///
239: /// </summary>
240: public static void Media(string path, int number)
241: {
242: Media(path, number, true, false);
243: }
244:
245: ////////////////////////////////////////////////////////////////////////////
246:
247: /// <summary>
248: ///
249: /// </summary>
250: public static void Media(string relativePath, int num, bool tempFolder, bool overwrite)
251: {
252: int i, n;
253: string relativePathFile, r;
254:
255: n = 0;
256:
257: HttpFileCollection hfc = HttpContext.Current.Request.Files;
258:
259: for (i = 0; i < hfc.Count; i++)
260: {
261: HttpPostedFile hpf = hfc[i];
262:
263: try
264: {
265: if (hpf.ContentLength > 0)
266: {
267: // check if the path_file is an image
268: if (hpf.ContentType == "image/jpeg" || hpf.ContentType == "image/pjpeg" || hpf.ContentType == "image/gif")
269: {
270: relativePathFile = relativePath + num + "_" + n + ".jpg";
271:
272: if (!overwrite)
273: {
274: while (Ia.Cl.Model.File.Exists(relativePathFile))
275: {
276: relativePathFile = relativePath + num + "_" + ++n + ".jpg";
277: }
278: }
279:
280: relativePathFile = relativePathFile.Replace(".jpg", "_temp.jpg");
281:
282: // replace the steps below with a stream operation
283: hpf.SaveAs(Ia.Cl.Model.Default.AbsolutePath() + relativePathFile);
284:
285: Ia.Cl.Model.Image.CreateGeneralUse(relativePathFile, relativePath, num, n);
286:
287: Ia.Cl.Model.File.Delete(relativePathFile);
288:
289: n++;
290: }
291: else if (hpf.ContentType == "audio/x-pn-realaudio")
292: {
293: }
294: else if (hpf.ContentType == "video/mp4")
295: {
296: relativePathFile = relativePath + num + "_" + n + ".mp4";
297:
298: if (!overwrite)
299: {
300: while (Ia.Cl.Model.File.Exists(relativePathFile))
301: {
302: relativePathFile = relativePath + num + "_" + ++n + ".mp4";
303: }
304: }
305:
306: // replace the steps below with a stream operation
307: hpf.SaveAs(Ia.Cl.Model.Default.AbsolutePath() + relativePathFile);
308:
309: n++;
310: }
311: else
312: {
313: }
314: }
315: }
316: catch (Exception ex)
317: {
318: #if DEBUG
319: r = "Error: " + ex.ToString();
320: #else
321: r = "Error: " + ex.Message;
322: #endif
323: }
324: finally
325: {
326: }
327: }
328: }
329:
330: ////////////////////////////////////////////////////////////////////////////
331: ////////////////////////////////////////////////////////////////////////////
332: }
333: }