예제 #1
0
        public ActionResult UpdateConResumeFile(FormCollection collection)
        {
            Int32  contactid = Convert.ToInt32(Request.Form["contactid"]);
            string resumes   = (Request.Form["resumes"]);

            // Checking no of files injected in Request object
            if (Request.Files.Count > 0)
            {
                try
                {
                    // Get all files from Request object
                    HttpFileCollectionBase files = Request.Files;
                    for (int i = 0; i < files.Count; i++)
                    {
                        HttpPostedFileBase file = files[i];
                        string             CustomFileName;
                        string             FilePath;
                        CustomFileName = contactid + Path.GetExtension(file.FileName);
                        var DbFilePath = context.Contacts.Where(x => x.contactid == contactid).Select(x => x.ResumeFilePath).FirstOrDefault();

                        if (DbFilePath != null)
                        {
                            string FullFilePath = Server.MapPath(DbFilePath);
                            if (System.IO.File.Exists(FullFilePath))
                            {
                                System.IO.File.Delete(FullFilePath);
                            }
                        }

                        // Get the complete folder path and store the file inside it.
                        FilePath = Server.MapPath("/UploadedResumes/" + CustomFileName);
                        file.SaveAs(FilePath);

                        //save the file in database
                        var stdComplete = context.Contacts.Where(x => x.contactid == contactid).SingleOrDefault();
                        stdComplete.ResumeFilePath       = "/UploadedResumes/" + CustomFileName;
                        context.Entry(stdComplete).State = EntityState.Modified;
                        context.SaveChanges();
                        if (resumes != null)
                        {
                            context.spupdatecontactresumes(resumes, contactid);
                            context.SaveChanges();
                        }
                    }
                    // Returns message that successfully uploaded
                    return(Json("File Uploaded Successfully!"));
                }
                catch (Exception ex)
                {
                    return(Json("Error occurred. Error details: " + ex.Message));
                }
            }
            else
            {
                if (resumes != null)
                {
                    context.spupdatecontactresumes(resumes, contactid);
                    context.SaveChanges();
                }
                return(Json("Resume Uploaded Successfully"));
            }
        }