SilverLight C#程序之:把文件转为二进制流保存到数据库(任意文件类型)

在C#程序里面我们经常会遇到文件上传的问题。

目前正在做一个SilverLight项目,也需要上传文件这样一个功能。。。

杯具的是SilverLight没有提供给我们够牛的上传控件,为此我们不得不,花费大量的精力自定义一个上传控件。。。

废话不多说看效果:

点击确定后

通过图片我们可以看到,上传控件可以上传任意一个文件。。。(忘记限制上传大小了=.=)

SilverLight代码

View Code
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="30"></RowDefinition>
</Grid.RowDefinitions>
<sdk:DataGrid x:Name="gridConAttachmentModel" Grid.Row="0" RowHeight="25"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="#FEFEFE"
HeadersVisibility="Column" BorderThickness="0,1,0,0" GridLinesVisibility="All"
AutoGenerateColumns="False" FrozenColumnCount="1" AreRowDetailsFrozen="True"
ItemsSource="{Binding ConAttachmentModelList}" SelectedItem="{Binding ConAttachmentModel}"
IsReadOnly="True">
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn Header="序号" Binding="{Binding ConAtt_OrderNum,Mode=TwoWay}" IsReadOnly="True" />
<sdk:DataGridTextColumn Header="附件名称" Binding="{Binding ConAtt_Name,Mode=TwoWay}" />
<sdk:DataGridTextColumn Header="附件资料" Binding="{Binding ConAtt_Name,Mode=TwoWay}">
</sdk:DataGridTextColumn>
<sdk:DataGridTextColumn Header="操作员编号" Binding="{Binding ConAtt_MakerId,Mode=TwoWay}" IsReadOnly="True" Visibility="Collapsed"></sdk:DataGridTextColumn>
<sdk:DataGridTextColumn Header="操作员名称" Binding="{Binding ConAtt_MakerName,Mode=TwoWay}" IsReadOnly="True" />
<sdk:DataGridTextColumn Header="备注" Binding="{Binding ConAtt_Remark,Mode=TwoWay}" />
<sdk:DataGridTemplateColumn Header="预览" Width="90" >
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<HyperlinkButton Content="查看" Margin="5" Click="HyperlinkButton_Click"></HyperlinkButton>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
</sdk:DataGrid.Columns>
</sdk:DataGrid>
<Button Height="20" Width="80" Margin="5" Content="添加文件" Grid.Row="1" HorizontalAlignment="Left" Command="{Binding UploadCommd}">
</Button>
</Grid>

CS代码

View Code
UploadCommd = new DelegateCommand(DoUploadCommd);
//定义上传命令

//上传方法void DoUploadCommd()
{
var check
= Container.Resolve<IContractUploadViewModel>();
//注册弹出窗页面
check.IContractListViewModel = this;
//弹出窗内的文件列表
check.View.Show();//显示弹出窗
}

子窗体Xaml代码

View Code
<Grid x:Name="LayoutRoot" Margin="2">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid x:Name="grid">
<Grid.RowDefinitions>
<RowDefinition Height="45"/>
<RowDefinition Height="60"/>

</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Height="25" FontSize="14"
          Grid.Column="1">请选择要上传的文件(可多选):</TextBlock>
<c1:C1FilePicker Name="file" Grid.Column="1" Grid.Row="1"
Height
="30" Margin="5"
                   Filter="All|*.*|Images|*.jpg;*.png;*.gif|Word|*.doc;*.docx|Excel|*.xls;*.xlsx"
FilterIndex
="2" Multiselect="True"/>

</Grid>
<Button x:Name="CancelButton" Content="取消" Command="{Binding CancelCommand}" Width="75" Height="23" HorizontalAlignment="Right" Margin="0,12,0,0" Grid.Row="1" />
<Button x:Name="OKButton" Content="确定" Command="{Binding SaveCommand}" Width="75" Height="23" HorizontalAlignment="Right" Margin="0,12,79,0" Grid.Row="1" />
</Grid>

子窗体CS代码

View Code
//两个命令 确定和退出
SaveCommand = new DelegateCommand(DoSave);
CancelCommand
= new DelegateCommand(() => View.Close());

/// <summary>
/// 保存数据
/// </summary>
void DoSave()
{
//获取上传控件里面的文件
var fileInfos = View.Conterl.file.SelectedFiles;
if (fileInfos == null)
{
MessageBox.Show(
"没有要上传的文件");
return;
}
//获取主界面 列表里面的行数
//用于修改 假如主界面有三个 现在上传的就给原基础上添加
//没有做防重复 事件
var i= IContractListViewModel.ConAttachmentModelList.Count;
ConAttachmentModelList
= new List<ConAttachmentModel>();
//遍历上传控件里面的所有文件
//本控件可以上传多个文件
foreach (var fileInfo in fileInfos)
{
//序号++
i++;
//读取文件
var fs = fileInfo.OpenRead();
//实例二进制转换器
var br = new BinaryReader(fs);
//写入二进制流
var filebyte = br.ReadBytes((int) fs.Length);
//实现一个保存到数据库的实体
ConAttachmentModel = new ConAttachmentModel
{
ConAtt_Files
= filebyte,
ConAtt_MakerName
= "管理员",
ConAtt_Name
= fileInfo.Name,
ConAtt_OrderNum
= i,
ConAtt_Remark
= "文件名: "+fileInfo.Name + "上传时间: " + DateTime.Now
};
ConAttachmentModelList.Add(ConAttachmentModel);
}
//关闭窗口
CancelCommand.Execute(null);
//遍历当前列表
//添加到主窗体
foreach (var conAttachmentModel in ConAttachmentModelList)
{
IContractListViewModel.ConAttachmentModelList.Add(conAttachmentModel);
} }

主窗体保存数据库

foreach (var conAttachmentModel in ContractModel.ConAttachmentModelList)
{
conAttachmentModel.ConAtt_Id
= Guid.NewGuid();
conAttachmentModel.Save();
}

大功告成!!!


作者:记忆逝去的青春
出处:http://www.cnblogs.com/lukun/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,如有问题,可以通过http://www.cnblogs.com/lukun/  联系我,非常感谢。

posted on 2011-04-12 14:17  记忆逝去的青春  阅读(2166)  评论(6编辑  收藏  举报