public bool MyProfile(FormCollection collec) { if (collec["ModelThanhVien"] != null) { ThanhVien thanhvien = JsonConvert.DeserializeObject <ThanhVien>(collec["ModelThanhVien"]); thanhvien.id = (Session["ThanhVien"] as ThanhVien).id; //thanhvien.id_LoaiThanhVien = 4; if (Request.Files.Count > 0) { Du_An_Y_Te.Controllers.API.FilesController FilesService = new API.FilesController(); List <int> dsTapTin = (List <int>)FilesService.SaveFile(Request.Files); TapTin tapntin = FilesService.GetById(dsTapTin.FirstOrDefault()); thanhvien.Picture = tapntin.DuongDan; } return(AccoutAPIControl.CapNhatThongTinCaNhan(thanhvien)); } //AccoutAPIControl.Login(new AccountViewModels.LoginViewModel() { TaiKhoan = thanhvien.TaiKhoan, MatKhau = thanhvien.MatKhau }); //if (AccoutAPIControl.Login(new AccountViewModels.LoginViewModel() { TaiKhoan = thanhvien.TaiKhoan, MatKhau = thanhvien.MatKhau })!=null) //{ //} //if (Session["ThanhVien"]!=null) //{ // return View("MyProfile"); //} //return RedirectToAction("index", "Home"); //return View("MyProfile"); return(false); }
public ActionResult GetFileById(int id) { //var stream = XTC.Helpers.ImageResizer.Resize(URL, 150, 200); //var result = new FileStreamResult(stream, "image/jpeg"); //return result; TapTin tapntin = FilesService.GetById(id); return(base.File(tapntin.DuongDan, tapntin.LoaiTapTin, tapntin.TenTapTin)); //return new System.Web.Mvc.FileStreamResult(new FileStream(URL, FileMode.Open), "image/*"); }
public ActionResult Create(TapTin model) { string kv; var sochungtu = TempData["sochungtu"].ToString(); kv = sochungtu; var mucdich = (from m in db.KhoanVay where m.SoChungTu == kv select m.idMucDichSuDung).First(); var tengiay = from h in db.dmGiayTo where h.idMucDichSuDung == (int)mucdich select new SelectListItem { Value = h.TenGiayTo, Text = h.TenGiayTo }; ViewBag.tengiay = tengiay; int idkv = (from h in db.KhoanVay where h.SoChungTu == sochungtu select h.id).First(); var file1 = Request.Files[0]; if (db.TapTin.FirstOrDefault(nv => (nv.tepDinhKem == file1.FileName)) != null) { TempData["Message"] = "File đã tồn tại"; return(Redirect("../taptin/Create?kv=" + sochungtu)); } else { if (ModelState.IsValid) { using (var scope = new TransactionScope()) { db.TapTin.Add(model); db.SaveChanges(); try { if (!String.IsNullOrEmpty(model.tepDinhKem)) { var file = Request.Files[0]; model.tepDinhKem = file.FileName; db.SaveChanges(); Upload(file); } scope.Complete(); return(Redirect("../khoanvay/details?id=" + idkv)); } catch (Exception e) { ModelState.AddModelError("tepDinhKem", e.Message); } } } } return(View()); }
public ActionResult Upload(TapTin model) { var TapTin = db.TapTin.Find(model.id); using (var scope = new TransactionScope()) try { var file = Request.Files[0]; TapTin.tepDinhKem = file.FileName; db.Entry(TapTin).State = EntityState.Modified; db.SaveChanges(); if (!String.IsNullOrEmpty(model.tepDinhKem)) { Upload(file); } scope.Complete(); return(RedirectToAction("Index", new { parent = TapTin.idTapTin })); } catch (Exception e) { ModelState.AddModelError("tepDinhKem", e.Message); } return(View(model)); }
public ActionResult Index(ObjFile doc, int cv) { CongViec c = db.CongViecs.Find(cv); ViewBag.title = c.TieuDe; foreach (var file in doc.files) { if (file.ContentLength > 0) { var fileName = Path.GetFileName(file.FileName); var filePath = Path.Combine(Server.MapPath("~/Files"), fileName); TapTin t = new TapTin(); t.MaCV = cv; t.DuongDan = file.FileName; t.NhanVienUp = SessionHelper.GetSession().id; db.TapTins.Add(t); db.SaveLog(SessionHelper.GetSession().id, "Đã upfile có tên: " + fileName + " cho công việc " + c.TieuDe, "Tập tin"); db.SaveChanges(); file.SaveAs(filePath); } } TempData["Message"] = "files uploaded successfully"; return(RedirectToAction("Index/" + cv)); }
public bool UploadFile(int iMaNhiemVuCode) { try { for (int i = 0; i < Request.Files.Count; i++) { var file = Request.Files[i]; if (file != null && file.ContentLength > 0) { string[] extHinh = new string[] { ".jpg", ".png" }; string[] extFile = new string[] { ".xls", ".xlsx", ".doc", ".docx", ".txt", ".zip", ".rar", ".sql", ".cs", ".js", ".css" }; using (var db = new dbnhiemvuEntities()) { // kiểm tra định dạng file là .jpg hoặc .png if (extHinh.Contains(Path.GetExtension(file.FileName).ToLower())) { // tạo thư mục lưu hình string spDirPath = Server.MapPath("~/Content/Images"); string targetDirpath = Path.Combine(spDirPath, iMaNhiemVuCode.ToString()); Directory.CreateDirectory(targetDirpath); // lấy tên file var fileName = Path.GetFileName(file.FileName); // tạo đường dẫn và lưu hình ảnh var path = Path.Combine(Server.MapPath($"~/Content/Images/{iMaNhiemVuCode}/"), fileName); file.SaveAs(path); // lưu hình ảnh vào cơ sở dữ liệu var image = new Hinh(); image.vDuongDan = $"/Content/Images/{iMaNhiemVuCode}/{fileName}"; image.iMaNhiemVuCode = iMaNhiemVuCode; image.iTrangThai = 1; db.Hinhs.Add(image); db.SaveChanges(); } else if (extFile.Contains(Path.GetExtension(file.FileName).ToLower())) { // tạo thư mục lưu file var spDirPath = Server.MapPath("~/Content/Files"); var targetDirpath = Path.Combine(spDirPath, iMaNhiemVuCode.ToString()); Directory.CreateDirectory(targetDirpath); // lấy tên file var fileName = Path.GetFileName(file.FileName); // tạo đường dẫn và lưu hình ảnh var path = Path.Combine(Server.MapPath($"~/Content/Files/{iMaNhiemVuCode}/"), fileName); file.SaveAs(path); // lưu hình ảnh vào cơ sở dữ liệu var taptin = new TapTin(); //kiểm tra xem tập tin này đã tồn tại hay chưa var checktaptin = db.TapTins.Where(x => x.vTenTapTin == fileName).Count(); if (checktaptin != 0) { return(true); } else { taptin.vDuongDan = $"/Content/Files/{iMaNhiemVuCode}/{fileName}"; taptin.iMaNhiemVuCode = iMaNhiemVuCode; taptin.vTenTapTin = fileName; taptin.iTrangThai = 1; db.TapTins.Add(taptin); db.SaveChanges(); } } } } } return(true); } catch { return(false); } }
public bool UploadFile(int iMaNhiemVuCode) { try { for (int i = 0; i < Request.Files.Count; i++) { var file = Request.Files[i]; if (file != null && file.ContentLength > 0) { string[] extHinh = new string[] { ".jpg", ".png" }; string[] extFile = new string[] { ".xls", ".xlsx", ".doc", ".docx", ".txt" }; using (var db = new dbnhiemvuEntities()) { // kiem tra dinh ddng file là .jpg hoac .png if (extHinh.Contains(Path.GetExtension(file.FileName).ToLower())) { var a = Path.GetExtension(file.FileName).ToLower(); // t?o thu m?c luu hình string spDirPath = Server.MapPath("~/Content/Images"); string targetDirpath = Path.Combine(spDirPath, iMaNhiemVuCode.ToString()); Directory.CreateDirectory(targetDirpath); // lay tên file var fileName = Path.GetFileName(file.FileName); // tao duong dan và luu hình anh var path = Path.Combine(Server.MapPath($"~/Content/Images/{iMaNhiemVuCode}/"), fileName); file.SaveAs(path); // luu hình anh vào co so du lieu var image = new Hinh(); image.vDuongDan = $"~/Content/Images/{iMaNhiemVuCode}/{fileName}"; image.iMaNhiemVuCode = iMaNhiemVuCode; image.iTrangThai = 1; db.Hinhs.Add(image); db.SaveChanges(); } else if (extFile.Contains(Path.GetExtension(file.FileName).ToLower())) { // tao thu muc luu file var spDirPath = Server.MapPath("~/Content/Files"); var targetDirpath = Path.Combine(spDirPath, iMaNhiemVuCode.ToString()); Directory.CreateDirectory(targetDirpath); // lay tên file var fileName = Path.GetFileName(file.FileName); // tao duong dan và luu hình anh var path = Path.Combine(Server.MapPath($"~/Content/Files/{iMaNhiemVuCode}/"), fileName); file.SaveAs(path); // luu hình anh vào co so du leu var taptin = new TapTin(); taptin.vTenTapTin = fileName; taptin.vDuongDan = $"~/Content/Files/{iMaNhiemVuCode}/{fileName}"; taptin.iMaNhiemVuCode = iMaNhiemVuCode; taptin.iTrangThai = 1; db.TapTins.Add(taptin); db.SaveChanges(); } } } } return(true); } catch (DbEntityValidationException e) { foreach (var eve in e.EntityValidationErrors) { foreach (var ve in eve.ValidationErrors) { Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } throw; } }