WEB API
https://1drv.ms/w/c/b3307ce20c008b2f/EQeBtEMBe5RBo3jslxC0KGgBuGj8b11FliK5_CP1bSio5w?e=xCTt7m
MVC 5
https://1drv.ms/w/c/b3307ce20c008b2f/EVc2Ncf6W_dOqJtq1cpBrJgBqaiHkq_gt2xlqySZ-lpv-g?e=unCUMV
--------------------------------------------------------------------------------------------------------------
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(Product product, IFormFile ImageFile)
{
if (ModelState.IsValid)
{
if (ImageFile != null)
{
var fileName = Path.GetFileNameWithoutExtension(ImageFile.FileName);
var extension = Path.GetExtension(ImageFile.FileName);
var imageName = fileName + "_" + DateTime.Now.ToString("yymmssfff") + extension;
var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/images", imageName);
using (var fileStream = new FileStream(path, FileMode.Create))
{
await ImageFile.CopyToAsync(fileStream);
}
product.ImagePath = imageName;
}
_context.Add(product);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(product);
}
-----------------------------------------------------------------------------------------------------
DELETE
--------------------------------------------------------------------------------------------
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(int id)
{
var product = await _context.Products.FindAsync(id);
if (product != null)
{
var imagePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/images", product.ImagePath);
if (System.IO.File.Exists(imagePath))
{
System.IO.File.Delete(imagePath);
}
_context.Products.Remove(product);
await _context.SaveChangesAsync();
}
return RedirectToAction(nameof(Index));
}
public async Task<IActionResult> Create(Category
category, IFormFile pic)
{
string FileName
= Path.GetFileName(pic.FileName);
string Ext =
Path.GetExtension(pic.FileName);
if
(Ext.ToLower() == ".jpg" || Ext == ".png" || Ext ==
".bmp" || Ext == ".jpeg" || Ext == ".gif")
{
string
FilePath = Path.Combine(Directory.GetCurrentDirectory(),
@"wwwroot\DataFiles", FileName);
using (var
fs = new FileStream(FilePath, FileMode.Create))
{
await
pic.CopyToAsync(fs);
category.CategoryImage = FileName;
}
}
if
(ModelState.IsValid)
{
//
Additional code for saving the category, if any, would go here
}
}
https://www.c-sharpcorner.com/article/upload-and-display-image-in-asp-net-core-3-1/
ReplyDeletehttps://www.pdsa.com/Home/BlogPost/134 images relateed
ReplyDeleteusing Microsoft.AspNetCore.Mvc;
ReplyDeletenamespace FileUploadInMVC.Controllers
{
public class FileUploadController : Controller
{
public IActionResult Index()
{
return View();
}
[HttpPost]
public async Task SingleFileUpload(IFormFile SingleFile)
{
if (ModelState.IsValid)
{
if (SingleFile != null && SingleFile.Length > 0)
{
var filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/uploads", SingleFile.FileName);
//Using Buffering
using (var stream = System.IO.File.Create(filePath))
{
// The file is saved in a buffer before being processed
await SingleFile.CopyToAsync(stream);
}
//Using Streaming
//using (var stream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
//{
// await SingleFile.CopyToAsync(stream);
//}
// Process the file here (e.g., save to the database, storage, etc.)
return View("UploadSuccess");
}
}
return View("Index");
}
}
}
https://www.asmak9.com/2017/12/aspnet-mvc5-full-calendar-jquery-plugin.html
ReplyDelete