demo20230512/Areas/admin/Controllers/PublicReferenceFileController.cs

82 lines
3.0 KiB
C#
Raw Normal View History

2023-05-12 10:20:28 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Weee.DAL;
using Weee.Models;
using PagedList;
namespace Weee.Areas.Admin.Controllers
{
public class PublicReferenceFileController : AdminControllerBase
{
public PublicReferenceFileController(WeeeDataContext db) : base(db)
{
}
public ActionResult Index(int page = 1)
{
var publicFiles = db.PublicReferenceFiles.ToList();
var onePageOfPublicFiles = publicFiles.ToPagedList(page, 10);
return View(onePageOfPublicFiles);
}
public ActionResult Edit(int id)
{
var file = db.PublicReferenceFiles.Find(id);
if (file == null)
file = new PublicReferenceFile();
return View(file);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(int id, PublicReferenceFile file)
{
if (ModelState.IsValid && id == file.ID)
{
var a = Request.Files["uploadedFile"];
var entry = db.Entry(file);
if (file.ID != 0)
{
entry.State = System.Data.Entity.EntityState.Modified;
entry.Property(x => x.CreatedTime).IsModified = false;
entry.Entity.UpdatedTime = DateTime.Now;
file.CreatedTime = entry.GetDatabaseValues().GetValue<DateTime>("CreatedTime");
}
else
{
entry.State = System.Data.Entity.EntityState.Added;
}
var postedFile = Request.Files["uploadedFile"];
if (postedFile.ContentLength != 0)
{
var storage = DependencyResolver.Current.GetService<Storage.AzureStorage>();
if (file.DownloadLink!=null&&file.DownloadLink!="") storage.DeleteFromAzure(file.DownloadLink);
//file.DownloadLink = storage.SaveToAzure(postedFile.InputStream, postedFile.FileName).ToString();//DL-3
string link = storage.SaveToAzure(postedFile.InputStream, postedFile.FileName, baseUrl).ToString();//DL-3
file.DownloadLink = link.Substring(link.IndexOf("Browser_Local")-1);//DL-3
}
else
{
entry.Property(x => x.DownloadLink).IsModified = false;
}
db.SaveChanges();
}
return RedirectToAction("index");
}
public ActionResult Delete(int id)
{
var toBeRemove = db.PublicReferenceFiles.Find(id);
db.PublicReferenceFiles.Remove(toBeRemove);
var storage = DependencyResolver.Current.GetService<Storage.AzureStorage>();
storage.DeleteFromAzure(toBeRemove.DownloadLink);
db.SaveChanges();
return RedirectToAction("Index");
}
}
}