شركة التطبيقات المتكاملة لتصميم النظم البرمجية الخاصة

Integrated Applications Programming Company

Skip Navigation LinksHome » Code Library » Upload

Public general use code classes and xml files that we've compiled and used over the years:

Handle file uploading functions.

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