How to store images on disk and link to database with EF?
I have a table
ID | ImagePath
-------------------
1 | ~/files/1.png
My model class has
public Int64 ID { ... }
public string ImagePath { ... }
I am trying to find an approach to store the image on a file. I was thinking in creating a byte[]
property and only write the file when I call SaveChanges
.
Is it possible? Is there some event that fires when changes are saved that I c开发者_如何学Pythonan use only in this case?
Or is there some better approach to store path
on the database and files
on the disk?
In one of my projects i store files just in folder and FilePath in database for linked entity(relation is 1-to-1). I just created entity for File with byte[] property and domain service for saving files on server. In InsertFile i call filehandler that saves bytes on disk.
public IQueryable<WebFile> GetFiles()
{
string path = "~/Upload";
List<WebFile> webFiles = new List<WebFile>();
if (string.IsNullOrEmpty(path)) return webFiles.AsQueryable();
DirectoryInfo di = new DirectoryInfo(HttpContext.Current.Server.MapPath(path));
foreach (FileInfo file in di.GetFiles())
{
webFiles.Add(new WebFile { FileName = file.Name });
}
return webFiles.AsQueryable();
}
public void InsertFile(WebFile file)
{
FileHandler.HandleFile(file);
}
Link in database is just filename (because there is one folder, no reason to storing full path)
FileHandler code:
public class FileHandler
{
public static void HandleFile(WebFile file)
{
string uploadDir = "~/Upload";
if (!string.IsNullOrEmpty(uploadDir))
{
if (uploadDir.IndexOf("~/") == 0)
uploadDir = HttpContext.Current.Server.MapPath(uploadDir);
if (uploadDir.LastIndexOf("/") == uploadDir.Length - 1)
uploadDir = uploadDir.Substring(0, uploadDir.Length - 1);
string fullFileName = string.Format("{0}/{1}", uploadDir, file.FileName);
if (File.Exists(fullFileName))
{
string ext = fullFileName.Substring(fullFileName.LastIndexOf("."));
string fName = fullFileName.Substring(0, fullFileName.LastIndexOf("."));
fullFileName = string.Format("{0}_1{1}", fName, ext);
}
File.WriteAllBytes(fullFileName, file.FileContent);
}
}
}
精彩评论