私はこれに本当に苦労しています。どんな助けでも大歓迎です。
フォルダー内の各ファイルを調べてデータベースに保存し、後で取り出して表示できるようにするコードが少し必要です。私はまだ表示部分に到達していません笑、まだデータベース内のファイルを取得しようとしています。
これまでのところ、エラーが発生しています
Value of type '1-dimensional array of Byte' cannot be converted to 'String'.
データベースのすべてのフィールドは、nvarchar(MAX)
Dim dir As New System.IO.DirectoryInfo("C:\Users\Will\Desktop\SUBARU")
For Each f As System.IO.FileInfo In dir.GetFiles("*.*")
Using db As New FileStoreAppDBEntities
Dim NewFile As New StoredFile
NewFile.FileName = f.Name
NewFile.FileSize = f.Length
'got trouble here
Dim ImageData As Byte() = System.IO.File.ReadAllBytes(f.FullName)
NewFile.FileContent = ImageData
'
NewFile.FileType = f.Extension
db.StoredFiles.AddObject(NewFile)
db.SaveChanges()
End Using
Next
おそらく私はこれをすべて間違っていますか?
助けてくれて本当にありがとうございます。
- 編集
これはいけそうです!
For Each f As System.IO.FileInfo In dir.GetFiles("*.*")
Using db As New FileStoreAppDBEntities
Dim NewFile As New StoredFile
NewFile.FileName = f.Name
NewFile.FileSize = f.Length
Dim filename As String = f.FullName
NewFile.FileContent = System.IO.File.ReadAllBytes(filename)
NewFile.FileType = f.Extension.ToLower
db.StoredFiles.AddObject(NewFile)
db.SaveChanges()
End Using
Next
ただし、パフォーマンスについてはわかりませんが、これによりファイルがメモリに配置され、ストリーミングの代わりに送信される可能性があると思いますか?
ファイルを取得するには:
Using db As New FileStoreAppDBEntities
Dim IDofFile As Integer = 31
Dim getFile = (From files In db.StoredFiles Where files.FileID = IDofFile Select files).SingleOrDefault
'getFile.FileName eg. mydocument.txt
System.IO.File.WriteAllBytes("C:\LocationToSaveTo\" & getFile.FileName, getFile.FileContent)
End Using
非常に大きなファイルでエラーが発生してSystem.OutOfMemoryException
いますが、小さなファイルでは完全に機能しています...
おそらく、ファイルをチャンクアップすることは可能でしょうか?