Ejemplo n.º 1
0
        public ActionResult UploadUserManual()
        {
            var formFile = (FormFile)Request.Form.Files[0];
            var stream   = formFile.OpenReadStream();

            byte[] data;
            using (var memoryStream = new MemoryStream()) {
                stream.CopyTo(memoryStream);
                data = memoryStream.ToArray();
            }

            var file = _userManualFileRepository.List().FirstOrDefault();

            if (file == null)
            {
                file = new ManualFile();
            }

            file.Name     = formFile.FileName;
            file.MimeType = formFile.ContentType;
            file.Data     = data;
            _userManualFileRepository.Update(file);
            return(Ok());
        }
Ejemplo n.º 2
0
        /*
         * Update machine
         * **/
        public IActionResult UpdateMachine(MachineFormData formData)
        {
            Machine m = _db.Machines.Include(u => u.CanUseMachines).Include(u => u.MachineAdmins).Include(u => u.CustomFields).Include(u => u.ManualFiles).Where(u => u.MachineId == formData.MachineId).FirstOrDefault();

            if (m == null)
            {
                var result = new
                {
                    status  = 400,
                    message = "Machine not exist"
                };
                return(Json(result));
            }
            else
            {
                string origin_name      = string.Empty;
                string manual_file_name = string.Empty;
                string manualFile       = string.Empty;
                string imageFile        = m.ImageFile;
                if (formData.StatusRemoveImage != 0)
                {
                    string _fileToBeDeleted = _env.WebRootPath + m.ImageFile;
                    // Check if file exists with its full path
                    if (System.IO.File.Exists(_fileToBeDeleted))
                    {
                        // If file found, delete it
                        System.IO.File.Delete(_fileToBeDeleted);

                        //Update name infomation file
                        imageFile = string.Empty;
                    }
                }

                string filename = string.Empty;
                var    files    = Request.Form.Files;
                if (files.Count() > 0)
                {
                    foreach (IFormFile file in files)
                    {
                        //IFormFile file = Request.Form.Files[0];
                        var formInputName = file.Name;

                        //string extention = Path.GetExtension(file.FileName);
                        origin_name = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
                        string str_random = RandomFilename();

                        filename = str_random + "_" + origin_name;
                        filename = EnsureCorrectFilename(filename);

                        FileStream fs = System.IO.File.Create(GetPathAndFilename(filename));
                        file.CopyTo(fs);
                        fs.Close();
                        if (formInputName == "ImageFile")
                        {
                            imageFile = "files/" + filename;
                        }
                        else
                        {
                            manualFile       = "files/" + filename;
                            manual_file_name = origin_name;
                        }
                    }
                }
                m.CategoryId         = formData.CategoryId;
                m.MachineNameEn      = formData.MachineNameEn;
                m.MachineNameJp      = formData.MachineNameJp;
                m.UnitPrice          = formData.UnitPrice;
                m.ReferenceAuth      = formData.ReferenceAuth;
                m.MakerNameEn        = formData.MakerNameEn;
                m.MakerNameJp        = formData.MakerNameJp;
                m.ModelEn            = formData.ModelEn;
                m.ModelJp            = formData.ModelJp;
                m.EquipmentNumber    = formData.EquipmentNumber;
                m.BusinessPerson     = formData.BusinessPerson;
                m.BusinessAddress    = formData.BusinessAddress;
                m.BusinessTel        = formData.BusinessTel;
                m.BusinessFax        = formData.BusinessFax;
                m.BusinessMail       = formData.BusinessMail;
                m.TechnicalAddress   = formData.TechnicalAddress;
                m.TechnicalFax       = formData.TechnicalFax;
                m.TechnicalPersion   = formData.TechnicalPersion;
                m.TechnicalMail      = formData.TechnicalMail;
                m.TechnicalTel       = formData.TechnicalTel;
                m.PurchaseDate       = formData.PurchaseDate;
                m.PurchaseCampany    = formData.PurchaseCampany;
                m.ExplanationEn      = formData.ExplanationEn;
                m.ExplanationJp      = formData.ExplanationJp;
                m.SpecEn             = formData.SpecEn;
                m.SpecJp             = formData.SpecJp;
                m.PlaceEn            = formData.PlaceEn;
                m.PlaceJp            = formData.PlaceJp;
                m.ExpendableSupplies = formData.ExpendableSupplies;
                m.URL       = formData.URL;
                m.URL2      = formData.URL2;
                m.URL3      = formData.URL3;
                m.Status    = 1;
                m.ShowOrder = 0;
                m.ImageFile = imageFile;
                //m.ManualFile = manualFile;
                _db.Machines.Update(m);

                if (manualFile.Length > 0)
                {
                    ManualFile mfile = new ManualFile();
                    mfile.MachineId      = m.MachineId;
                    mfile.ManualFileName = manualFile;
                    mfile.DisplayName    = manual_file_name;
                    m.ManualFiles.Add(mfile);
                    _db.SaveChanges();
                }

                m.MachineAdmins.Clear();
                _db.SaveChanges();
                int[] arrUserAdminId = null;
                if (formData.charge_user != null && formData.charge_user.Length > 0)
                {
                    arrUserAdminId = formData.charge_user.Split(',').Select(n => Convert.ToInt32(n)).ToArray();
                    foreach (int i in arrUserAdminId)
                    {
                        var machineAdmin = new MachineAdmin();
                        machineAdmin.MachineId = m.MachineId;
                        machineAdmin.UserId    = i;
                        m.MachineAdmins.Add(machineAdmin);
                    }
                    _db.SaveChanges();
                }
                m.CanUseMachines.Clear();
                _db.SaveChanges();
                int[] arrUserId = null;
                if (formData.use_user != null && formData.use_user.Length > 0)
                {
                    arrUserId = formData.use_user.Split(',').Select(n => Convert.ToInt32(n)).ToArray();
                    foreach (int i in arrUserId)
                    {
                        var canUseMachine = new CanUseMachine();
                        canUseMachine.MachineId = m.MachineId;
                        canUseMachine.UserId    = i;
                        m.CanUseMachines.Add(canUseMachine);
                    }
                    _db.SaveChanges();
                }

                //m.CustomFields.Clear();
                //_db.SaveChanges();
                int[]    arr_Check        = null;
                string[] arr_item_name_jp = null;
                string[] arr_item_name_en = null;
                int[]    arr_item         = null;
                if (formData.item_name_jp != null && formData.item_name_jp.Length > 0)
                {
                    arr_item         = formData.ItemId.Split(',').Select(n => Convert.ToInt32(n)).ToArray();
                    arr_Check        = formData.arrCheck.Split(',').Select(n => Convert.ToInt32(n)).ToArray();
                    arr_item_name_jp = formData.item_name_jp.Split(',').ToArray();
                    arr_item_name_en = formData.item_name_en.Split(',').ToArray();

                    for (int c = 0; c < arr_item_name_jp.Length; c++)
                    {
                        if (arr_item[c] > 0)
                        {
                            CustomField cust = _db.CustomFields.Find(arr_item[c]);
                            if (cust != null)
                            {
                                cust.FieldNameEn = arr_item_name_en[c];
                                cust.FieldNameJp = arr_item_name_jp[c];
                                cust.Required    = (arr_Check[c] == 1) ? true : false;
                                _db.CustomFields.Update(cust);
                            }
                        }
                        else
                        {
                            var custome = new CustomField();
                            custome.FieldNameEn = arr_item_name_en[c];
                            custome.FieldNameJp = arr_item_name_jp[c];
                            custome.MachineId   = m.MachineId;
                            custome.Required    = (arr_Check[c] == 1) ? true : false;
                            m.CustomFields.Add(custome);
                        }
                    }
                    _db.SaveChanges();
                }

                _db.SaveChanges();
                var result = new
                {
                    status  = 200,
                    message = "Update Success"
                };
                return(Json(result));
            }
        }