Пример #1
0
        /// <summary>
        /// This function downloads the file from a given location
        /// </summary>
        /// <param name="doc"></param>
        /// <param name="deleteExisting"></param>
        /// <param name="saveToDb"></param>
        /// <returns></returns>
        private int DownloadAndSaveDocument(Document doc, bool deleteExisting = false, bool saveToDb = true)
        {
            try
            {
                using (var wc = new WebClient())
                {
                    // Get the document path and creating directory
                    var path = GetDocumentLocation(doc.DocumentTypeValue, doc.RefId, doc.SubRefId).Replace("/", "\\");
                    if (!Directory.Exists(path))
                        Directory.CreateDirectory(path);
                    doc.FileName = Path.GetFileName(doc.UploadUrl);
                    // for the cv change the filename to cv
                    if (doc.DocumentTypeValue == 4)
                    {
                        doc.FileName = "cv" + Path.GetExtension(doc.UploadUrl);
                    }

                    if (deleteExisting)
                    {
                        DeleteFilesForDocType(path, doc);
                        path = path + doc.FileName;
                    }
                    else
                    {
                        path = path + doc.FileName;
                        //check for same file
                        path = GetFileName(path);
                        doc.FileName = Path.GetFileName(path);
                    }
                    var f = HttpContext.Current.Server.MapPath(doc.UploadUrl);
                    // Download the Web resource and save it into the current filesystem folder.
                    if (doc.UploadUrl.StartsWith("http"))
                        wc.DownloadFile(doc.UploadUrl, path);
                    else if (File.Exists(HttpContext.Current.Server.MapPath(doc.UploadUrl)))
                        File.Copy(HttpContext.Current.Server.MapPath(doc.UploadUrl), path);
                    else
                        return 0;
                    // Save document

                    if (saveToDb)
                    {
                        return AddDocument(doc);
                    }

                }
            }
            catch (Exception e)
            {
                // ignored
            }
            return -1;
        }
Пример #2
0
 /// <summary>
 /// This function deletes the records from daatbase for specified doc type to handle duplications
 /// </summary>
 /// <param name="path"></param>
 /// <param name="doc"></param>
 private void DeleteFilesForDocType(string path, Document doc)
 {
     var context = new dbDataContext();
     try
     {
         List<tbl_Document> documents;
         //delete entries from db
         if (doc.SubRefId > -1)
         {
             documents = (from t in context.tbl_Documents where t.DocumentTypeValue == doc.DocumentTypeValue && t.RefId == doc.RefId && t.SubRefId == doc.SubRefId select t).ToList();
         }
         else
             documents = (from t in context.tbl_Documents where t.DocumentTypeValue == doc.DocumentTypeValue && t.RefId == doc.RefId select t).ToList();
         if (documents.Count > 0)
         {
             context.tbl_Documents.DeleteAllOnSubmit(documents);
             context.SubmitChanges();
         }
         //delete from actual location
         Array.ForEach(Directory.GetFiles(path), File.Delete);
     }
     catch (Exception)
     {
         //Ignore
     }
 }
Пример #3
0
 /// <summary>
 /// Add document into database
 /// </summary>
 /// <param name="doc"></param> 
 private int AddDocument(Document doc)
 {
     var context = new dbDataContext();
     var document = new tbl_Document
     {
         DocumentTypeValue = doc.DocumentTypeValue,
         FileName = doc.FileName,
         RefId = doc.RefId,
         DocumentPath = GetDocumentLocation(doc.DocumentTypeValue, doc.RefId, doc.SubRefId, true) + doc.FileName,
         CreatedDate = DateTime.Now,
         SubRefId = doc.SubRefId,
         DocGuid = Guid.NewGuid().ToString(),
         UploadedBy = doc.UploadedBy
     };
     context.tbl_Documents.InsertOnSubmit(document);
     context.SubmitChanges();
     return document.DocumentId;
 }
Пример #4
0
 /// <summary>
 /// This file create the file using posted file
 /// </summary>
 /// <param name="doc"></param>
 /// <param name="deleteExisting"></param>
 /// <param name="saveToDb"></param>
 /// <returns></returns>
 private int CreateFileFromFileUploader(Document doc, bool deleteExisting = false, bool saveToDb = true)
 {
     try
     {
         // Get the document path and creating directory
         var path = GetDocumentLocation(doc.DocumentTypeValue, doc.RefId, doc.SubRefId).Replace("/", "\\");
         // throw new Exception(path);
         if (!Directory.Exists(path))
             Directory.CreateDirectory(path);
         doc.FileName = Path.GetFileName(doc.PostedFile.FileName);
         // for the cv change the filename to cv
         if (doc.DocumentTypeValue == 4)
         {
             doc.FileName = "cv" + Path.GetExtension(doc.PostedFile.FileName);
         }
         if (deleteExisting)
         {
             DeleteFilesForDocType(path, doc);
             path = path + doc.FileName;
         }
         else
         {
             path = path + doc.FileName;
             //check for same file
             path = GetFileName(path);
             doc.FileName = Path.GetFileName(path);
         }
         //save the file inside the posted file
         doc.PostedFile.SaveAs(path);
         // Save document
         if (saveToDb)
         {
             return AddDocument(doc);
         }
     }
     catch (Exception)
     {
         // ignored
     }
     return 0;
 }