public IActionResult OnPost() { if (!ModelState.IsValid) { return(Page()); } // string file = Path.GetFileName(Photo.FileName); if (Photo != null) { string file = Guid.NewGuid().ToString("N") + new FileInfo(Photo.FileName).Extension; var filename = Path.Combine(webHostEnvironment.WebRootPath, "uploads", file); using (var stream = new FileStream(filename, FileMode.Create)) { Photo.CopyTo(stream); } Customer.Photo = Path.Combine("/uploads", file); } customerService.Update(Customer); return(RedirectToPage("./Index")); }
public IActionResult Create(EmployeeCreateViewModel model) { if (ModelState.IsValid) { string UniqueFile = null; if (model.Photos != null && model.Photos.Count > 0) { foreach (IFormFile Photo in model.Photos) { string UploadFolder = Path.Combine(hostingEnvironment.WebRootPath, "images"); UniqueFile = Guid.NewGuid().ToString() + "_" + Photo.FileName; string FilePath = Path.Combine(UploadFolder, UniqueFile); Photo.CopyTo(new FileStream(FilePath, FileMode.Create)); } } Employee newEmployee = new Employee { Name = model.Name, Email = model.Email, Photos = UniqueFile, Capcity = model.Capacity, Location = model.Location, Price = model.Price }; _employeeRespository.Add(newEmployee); //saved to data base table return(RedirectToAction(nameof(Property), new { id = newEmployee.Id })); } return(View()); }
//提炼共享的私有方法,处理文件信息 /// <summary> /// 将图片保存指定的路径中,并返回唯一的文件名 /// </summary> /// <returns></returns> private string ProcessUploadedFile(StudentCreateViewModel model) { string uniqueFileName = null; if (model.Photos != null && model.Photos.Count > 0) { foreach (var Photo in model.Photos) { //必须将图像上传到WWWroot中的images文件夹 //而要获取wwwroot文件夹的路径,我们需要注入ASP.NET Core提供的HostingEnvironment服务 string uploadFolder = Path.Combine(hostingEnvironment.WebRootPath, "images"); //为了确保文件名是唯一的,我们在文件名后附加一个新的GUID值和一个下划线 uniqueFileName = Guid.NewGuid().ToString() + "_" + Photo.FileName;//添加全局唯一标识符 string filePath2 = Path.Combine(uploadFolder, uniqueFileName); //使用IFormFile接口提供的CopyTo()方法将文件复制到wwwroot/images文件夹 //因为使用的非托管资源,所以需要手动释放.非托管资源垃圾回收器无法自动释放. using (var fileStrem = new FileStream(filePath2, FileMode.Create)) { //这里使用 Photo.CopyTo(fileStrem); //这里释放 } //Photo.CopyTo(new FileStream(filePath2, FileMode.Create));//流处理FileMode继承与IDisposable } } return(uniqueFileName); }
public IActionResult Create(EmployeeCreateViewModel model) { if (ModelState.IsValid) { string uniqueFileName = null; if (model.Photos != null && model.Photos.Count() > 0) { foreach (IFormFile Photo in model.Photos) { string uploadsFolder = Path.Combine(webHostEnvironment.WebRootPath, "images"); uniqueFileName = Guid.NewGuid().ToString() + "_" + Photo.FileName; string pathFile = Path.Combine(uploadsFolder, uniqueFileName); Photo.CopyTo(new FileStream(pathFile, FileMode.Create)); } } Employee newEmp = new Employee { Name = model.Name, Email = model.Email, Department = model.Department, PhotoPath = uniqueFileName }; _mockEmployeeRepository.AddEmployee(newEmp); return(RedirectToAction("Details", new { id = newEmp.Id })); } return(View()); }
public void OnPost() { var filePath = Env.WebRootPath + "/photos/" + Photo.FileName; Console.WriteLine(filePath); using (var stream = System.IO.File.Create(filePath)) { Photo.CopyTo(stream); // Make a copy of uploaded file } }
public object Clone() { var doc = (Document)this.MemberwiseClone(); if (Photo != null) { doc.Photo = new byte[Photo.Length]; Photo.CopyTo(doc.Photo, 0); } return(doc); }
private string ProcessUploadedFile() { if (Photo == null) { return(null); } var uploadsFolder = Path.Combine(_env.WebRootPath, "images"); var uniqueFileName = Guid.NewGuid() + "_" + Photo.FileName; var filePath = Path.Combine(uploadsFolder, uniqueFileName); using var fileStream = new FileStream(filePath, FileMode.Create); Photo.CopyTo(fileStream); return(uniqueFileName); }
private string UploadingFilePhoto() { string uniqueFileName = null; if (Photo != null) { string uploadSFolder = Path.Combine(webHostEnvironment.WebRootPath, "images"); uniqueFileName = Guid.NewGuid().ToString() + "_" + Photo.FileName; string filePath = Path.Combine(uploadSFolder, uniqueFileName); using (var fileStream = new FileStream(filePath, FileMode.Create)) { Photo.CopyTo(fileStream); } } return(uniqueFileName); }
private string ProcessUploadFile() { string uniquefilename = null; if (Photo != null) { string uploadfolder = Path.Combine(webHostEnvironment.WebRootPath, "Images"); uniquefilename = Guid.NewGuid().ToString() + "_" + Photo.FileName; string filepath = Path.Combine(uploadfolder, uniquefilename); using (var fileStrim = new FileStream(filepath, FileMode.Create)) { Photo.CopyTo(fileStrim); } } return(uniquefilename); }
} //these are the notification preferences when the user is creating an account to the teacher DB private string ProcessUploadedFile() { string uniqueFileName = null; if (Photo != null) { string uploadsFolder = Path.Combine(webHostEnvironment.WebRootPath, "images"); uniqueFileName = Guid.NewGuid().ToString() + "_" + Photo.FileName; string filePath = Path.Combine(uploadsFolder, uniqueFileName); using (var fileStream = new FileStream(filePath, FileMode.Create)) { Photo.CopyTo(fileStream); } } //allows user to add, update or remove a photo from their profile. return(uniqueFileName); }
//метод обработки фотографии private string ProcessUploadFile() { string uniqueFileName = null; //уникальное имя if (Photo != null) { string uploadsFolder = Path.Combine(_webHostEnvironment.WebRootPath, "images"); //1-е возвращает путь к фото, сохраняет в папку images uniqueFileName = Guid.NewGuid().ToString() + "_" + Photo.FileName; //уникальное имя фото присваеивается код Guid = код_имя. string filePath = Path.Combine(uploadsFolder, uniqueFileName); //комбинирование полного пути //сохранение фото на сервере using (var fs = new FileStream(filePath, FileMode.Create)) // 1-е куда сохранять фото, 2-е создает файл { Photo.CopyTo(fs); // копируем фото на сервер по обозначенному пути } } return(uniqueFileName); // возвращаем уникальное имя }
public string ProcessUploadedFile() { string uniqueFileName = null; if (Photo != null) { string uploadsFolder = Path.Combine(webHostEnvironment.WebRootPath, "siteassets/users"); uniqueFileName = Guid.NewGuid().ToString() + "_" + Path.GetFileName(Photo.FileName); string filePath = Path.Combine(uploadsFolder, uniqueFileName); using (var fileStream = new FileStream(filePath, FileMode.Create)) { Photo.CopyTo(fileStream); } } return(uniqueFileName); }
private string ProcessUploadedFile() { string uniqueFileName = null; if (Photo != null) { string uploadsFolder = Path.Combine(_webHostEnvironment.WebRootPath, "images"); uniqueFileName = Guid.NewGuid().ToString() + "_" + Photo.FileName; string filePath = Path.Combine(uploadsFolder, uniqueFileName); using (var fs = new FileStream(filePath, FileMode.Create)) // автоматически вызывает Dispose() и высвобождает ресурсы { Photo.CopyTo(fs); } } return(uniqueFileName); }
private string ProcessUploadedFile() { string uniqueFileName = null; if (Photo != null) { string uploadsFolder = Path.Combine(hostingEnvironment.WebRootPath, "images"); string[] fileName = Photo.FileName.Split("\\"); uniqueFileName = Guid.NewGuid().ToString() + "_" + fileName[fileName.Length - 1].ToString(); string filePath = Path.Combine(uploadsFolder, uniqueFileName); using (var fileStream = new FileStream(filePath, FileMode.Create)) { Photo.CopyTo(fileStream); } } return(uniqueFileName); }
private string ProcessUploadedFile() { string uniqueFileName = null; if (Photo != null) { string uploadsFolder = Path.Combine(_environment.WebRootPath, "images"); uniqueFileName = Guid.NewGuid().ToString() + "_" + Photo.FileName; string filePath = Path.Combine(uploadsFolder, uniqueFileName); using (var fs = new FileStream(filePath, FileMode.Create)) { Photo.CopyTo(fs); } } return(uniqueFileName); }
/// <summary> /// Скопировать новый файл в БД /// Вернуть имя нового файла /// </summary> /// <returns></returns> private string GetUploadedFilename() { string result = null; if (Photo != null) { //Получаем конечную папку string uploadsFolder = Path.Combine(_webHostEnvironment.WebRootPath, "images"); //Получаем уникальное имя файла с фотографией result = $"{Guid.NewGuid()}{Photo.FileName}"; //Получаем полный путь файла на сервере string filePath = Path.Combine(uploadsFolder, result); //Копируем файл с фотографией на сервер using var fs = new FileStream(filePath, FileMode.Create); Photo.CopyTo(fs); } return(result); }
/// <summary> /// Combines the file path and the image name to build a URL /// </summary> /// <param name="uploadsFolder">Location where to upload a photo</param> /// <returns>A unique File Name</returns> private string ProcessUploadedFile(string uploadsFolder) { if (Photo == null) { return(null); } string uniqueFileName = null; uploadsFolder = Path.Combine("wwwroot", uploadsFolder); uniqueFileName = Photo.FileName; string filePath = Path.Combine(uploadsFolder, uniqueFileName); using (var fileStream = new FileStream(filePath, FileMode.Create)) { Photo.CopyTo(fileStream); } return(uniqueFileName); }
//ћетод дл¤ сохранение фотографии и добавление уникального имени на фото и проверка на null private string ProcessUploadFile() { string uniqueFileName = null; if (Photo != null) { //ѕуть wwwroot images дл¤ сохранение фотографии string uploadsFolder = Path.Combine(_webHostEnvironment.WebRootPath, "images"); //”никально им¤ будет генерироватьс¤ и добавл¤т нижный пробел uniqueFileName = Guid.NewGuid().ToString() + "_" + Photo.FileName; string filePath = Path.Combine(uploadsFolder, uniqueFileName); // —охранение фотографии на сервер using (var fileStreem = new FileStream(filePath, FileMode.Create)) { Photo.CopyTo(fileStreem); } } return(uniqueFileName); }
/// <summary> /// 拼接图片存放路径,复制图片到指定路径 /// </summary> /// <param name="model"></param> /// <returns></returns> private string ProcessUploadedFile(StudentCreatViewModel model) { string uniqueFileName = null; if (model.Photos != null || model.Photos.Count > 0) { //string uploadsFolder = Path.Combine(hostingEnvironment.WebRootPath, "images");//wwwroot下的images //uniqueFileName = Guid.NewGuid().ToString() + "_" + model.Photo.FileName;//特殊唯一文件名 //string filePath = Path.Combine(uploadsFolder, uniqueFileName);//拼接出文件目录文件名 //model.Photo.CopyTo(new FileStream(filePath, FileMode.Create));//复制图片到指定目录 foreach (var Photo in model.Photos) { string uploadsFolder = Path.Combine(hostingEnvironment.WebRootPath, "images"); //wwwroot下的images uniqueFileName = Guid.NewGuid().ToString() + "_" + Photo.FileName; //特殊唯一文件名 string filePath = Path.Combine(uploadsFolder, uniqueFileName); //拼接出文件目录文件名 using (var copyFilePath = new FileStream(filePath, FileMode.Create)) { Photo.CopyTo(copyFilePath);//复制图片到指定目录 } } } return(uniqueFileName); }
public object Clone() { var clone = (Person)this.MemberwiseClone(); if (Photo != null) { clone.Photo = new byte[Photo.Length]; Photo.CopyTo(clone.Photo, 0); } if (DoorList != null) { clone.DoorList = new List <string>(DoorList); } if (Document != null) { clone.Document = (Document)Document.Clone(); } if (AccessCard != null) { clone.AccessCard = new ObservableCollection <AddressValue <CardStates> >(AccessCard); } return(clone); }