Я разобрался в проблеме.Вот мой код -
private void AddFile()
{
if (!fupFile.HasFile)
{
lblMessage.Text = "Please select a file.";
return;
}
var data = new byte[(int) fupFile.FileContent.Length];
fupFile.FileContent.Read(data, 0, data.Length);
if (fupFile.FileContent.Length > 0)
{
var repositoryFile = new Repository
{
ID = Guid.NewGuid(),
Name = Path.GetFileName(fupFile.PostedFile.FileName),
Data = System.Text.Encoding.ASCII.GetBytes("0x00")
};
RepositoryController.AddItem(repositoryFile, data); // Calling DAL class.
}
}
// DAL method
public static void AddItem(RepositoryFile repository, byte[] data)
{
using (var scope = new TransactionScope())
{
using (var db = new MyEntities()) // DBContext
{
db.RepositoryTable.AddObject(repository);
db.SaveChanges();
}
using (var con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
using (var cmd = new SqlCommand(string.Format("SELECT Data.PathName(), GET_FILESTREAM_TRANSACTION_CONTEXT() FROM dbo.RepositoryTable WHERE ID='{0}'", repository.ID), con)) // "Data" is the column name which has the FILESTREAM. Data.PathName() gives me the local path to the file.
{
cmd.Connection.Open();
using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
{
var path = reader.GetString(0);
var transactionContext = reader.GetSqlBytes(1).Buffer;
var fileStream = new SqlFileStream(path, transactionContext, FileAccess.Write);
fileStream.Write(contents, 0, contents.Length); //Write contents to the file.
fileStream.Close();
}
}
}
scope.Complete();
}
}