Exemplo n.º 1
0
 private void UploadFile(NewsModel model, List <HttpPostedFileBase> filesUpdoad)
 {
     if (filesUpdoad != null && filesUpdoad.Any())
     {
         foreach (HttpPostedFileBase file in filesUpdoad)
         {
             if (file != null && file.ContentLength > 0)
             {
                 try
                 {
                     var pathfoder = Path.Combine(Server.MapPath(@"~/" + DOCUMENT_PATH), model.Type.ToString());
                     if (!Directory.Exists(pathfoder))
                     {
                         Directory.CreateDirectory(pathfoder);
                     }
                     string filePath = Path.Combine(pathfoder, Path.GetFileName(file.FileName));
                     file.SaveAs(filePath);
                     //save file to db
                     var fileSave = new ServerFile
                     {
                         ObjectId     = model.Id,
                         ObjectType   = model.GetType().ToString(),
                         Path         = string.Format("{0}/{1}/{2}", DOCUMENT_PATH, model.Type.ToString(), file.FileName),
                         FileName     = file.FileName,
                         FileSize     = file.ContentLength,
                         FileMimeType = file.ContentType
                     };
                     freightServices.insertServerFile(fileSave);
                 }
                 catch (Exception ex)
                 {
                     Logger.LogError(ex);
                 }
             }
         }
     }
 }