我正在考虑将LiteDb用于C#作为文档存储系统。
只要我知道文件的ID,我就可以上传和下载 - 但我也希望能够选择搜索它。
LiteDb文档提供了此命令,用于搜索存储在特定'目录中的所有文件。在Db内;
echo 'export PATH="$HOME/.composer/vender/bin:$PATH"' >> ~/.bashrc
但是,我无法弄清楚如何处理files变量。我无法将其转换为可用的字符串等。
我想将它添加到listview中。例如。
有没有人有使用LiteDb和文件搜索的经验?
答案 0 :(得分:2)
回答得有点晚,但是它可能仍然有用。搜索文件的更好方法是访问“ _files”集合,该集合将使您可以搜索文件的元数据:
class _files
{
public string Id { get; set; }
public string filename { get; set; }
public string mimeType { get; set; }
public long length { get; set; }
public int chunks{ get; set; }
public DateTime uploadDate { get; set; }
public object metadata { get; set; } //You could replace object with a custom class
}
然后可以使用“ _files”类访问文件信息以进行搜索。在这种情况下,它将检索文件名中带有“ somefile”的所有文件:
var files = new LiteDatabase("myDatabase.db")
.GetCollection<_files>() //Accesses the _files collection where file info is stored
.Find(file => file.Filename.Contains("somefile")); //Find files with "somefile" in the filename
然后您可以遍历结果以访问文件的信息:
foreach(var searchResult in files)
{
var fileInfo = $"Id: {searchResult.Id} | Filename: {searchResult.filename} | Length: {searchResult.length}";
Console.WriteLine(fileInfo);
}
这将输出类似于:
Id: file1 | Filename: important.doc | Length: 73453
Id: file2 | Filename: less_important.doc | Length: 3476
此外,不要忘记为经常搜索的属性创建索引。例如,在上面的示例中,我搜索了文件名。我可以为filename属性创建一个索引,如下所示:
new LiteDatabase("myDatabase.db")
.GetCollection<_files>()
.EnsureIndex(files => files.filename); //Create an index for filename
答案 1 :(得分:0)
我设法解决了问题....如果对其他人有用,我会分享。
listView1.Items.Clear();
try
{
//"" is a blank search which will return all files.
//But could easily be a value from a textbox. It is loaded into the Array Files
var files = db.FileStorage.Find("").ToArray();
//go through each obj in the array and abstract FileName, ID and
//Created date, adding them each to a new array (mylist, mylist1 etc.)
foreach (object obj in files)
{
mylist = files.Select(I => Convert.ToString(I.Filename)).ToArray();
mylist1 = files.Select(I => Convert.ToString(I.UploadDate)).ToArray();
mylist2 = files.Select(I => Convert.ToString(I.Id)).ToArray();
}
//loop through the mylist arrays an create usable strings for each value.
for (int i = 0; i < mylist.Length; i++)
{
name = mylist[i].ToString();
datecreated = mylist1[i].ToString();
id = mylist2[i].ToString();
//add each value to a listview
listView1.Items.Add(new ListViewItem(new string[] { name, datecreated,id }));
}
答案 2 :(得分:0)
我有一种更简单的上传文件的方式,例如,如果要将用户的个人资料照片上传到LiteDB,则我为文档和文件使用相同的ID。为此,我首先创建一个新的ObjectID。然后,我得到图片并以fileId作为我刚创建的ID上载。为了验证我是否确实上传了文件,我将其下载到特殊目录文件夹中的本地文件夹中。然后,我获取本地文件夹的filePath并将其保存为“用户配置文件”文档的一部分。因此,一旦用用户填充了视图,就可以使用ID从本地目录或直接从LiteDB中检索文件。
您可以访问我的YouTube频道,以获取有关LiteDB的完整教程:https://www.youtube.com/watch?v=fiwJ0XkHIZg 或
您可以从我的GitHub存储库中获取完整的源代码:https://github.com/Parables/CMS。
以下是一些可以从LiteDB上传,查找和删除文件的功能:
#Region "Set Profile Picture"
Private Sub pbxPicture_Click(sender As Object, e As EventArgs) Handles pbxPicture.Click
With OpenFileDialog1
.AddExtension = True
.CheckFileExists = True
.CheckPathExists = True
.Multiselect = False
.ShowDialog()
.Title = "Select a picture for the Member"
End With
End Sub
Private Sub OpenFileDialog1_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
Dim ofdFileName As String = OpenFileDialog1.FileName.ToString
Try
FindPhoto(UploadImage(ofdFileName))
Catch ex As Exception
MsgBox(ex.Message)
End Try
' DeleteUploadedImage(picName)
End Sub
#End Region
#Region "Picture Upload, Find and Delete"
Dim uploadURL As String = ""
Dim downloadURL As String = ""
Private Function UploadImage(fileName) As String
Try
Dim db As New LiteDatabase(CMSDB)
uploadURL = $"{MembersPhotoStorage}{ID}"
db.FileStorage.Upload(uploadURL, fileName)
Catch ex As Exception
MsgBox(ex.Message)
End Try
Return uploadURL
End Function
Private Function FindPhoto(FileID As String) As String
Dim File As LiteFileInfo = Nothing
Try
Dim db As New LiteDatabase(CMSDB)
File = db.FileStorage.FindById(FileID)
My.Computer.FileSystem.CreateDirectory(MembersPhotoDir)
downloadURL = $"{MembersPhotoDir}{ID}.png"
File.SaveAs(downloadURL, True)
pbxPicture.ImageLocation = downloadURL
Catch ex As Exception
MsgBox(ex.Message)
End Try
Return downloadURL
End Function
Private Sub DeleteUploadedImage()
Try
Dim db As New LiteDatabase(CMSDB)
db.FileStorage.Delete($"{MembersPhotoStorage}{ID}")
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
#End Region