我在我的 Web 应用程序中上传和下载 Word 文件。我关注了两篇文章来帮助我实现这一目标:
如何:导入和存储数据文件(这个只讨论上传,但过程类似)
在 Lightswitch 2011 中上传和下载文件
在我的解决方案中,为了对称起见,上传和下载都遵循第一篇文章中的模式。我并不真正关心第二篇文章处理下载方面的方式。我不记得第二篇文章中提到的参考资料是否对我做事的方式是必要的。我的项目中有这些参考资料,但我知道我将它们用于其他事情。并且至少在 VS2013 中,不需要卸载和重新加载项目。
本质上,您需要创建自定义 Silverlight 控件来添加此功能。因此,将.xaml
文件添加到您的用户代码中。两篇文章都有应该在里面的例子,我的看起来像这样:
<controls:ChildWindow x:Class="LightSwitchApplication.DownloadFileWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
Width="380" Height="125"
Title="Select Where to Save File" >
<Grid x:Name="LayoutRoot" Margin="2">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Button x:Name="CancelButton" Content="Cancel" Click="CancelButton_Click" Width="75" Height="25" HorizontalAlignment="Right" Margin="0,15,0,0" Grid.Row="1" />
<Button x:Name="OKButton" Content="OK" Click="OKButton_Click" Width="75" Height="25" HorizontalAlignment="Right" Margin="0,15,80,0" Grid.Row="1" />
<Button Content="Browse" Height="25" HorizontalAlignment="Left" Margin="275,10,0,0" Name="BrowseButton" VerticalAlignment="Top" Width="75" Click="BrowseButton_Click" />
<TextBox Height="25" HorizontalAlignment="Left" Margin="10,10,0,0" Name="FileTextBox" VerticalAlignment="Top" Width="250" IsEnabled="True"/>
</Grid>
</controls:ChildWindow>
VS2013(我正在使用)和 LightSwitch 2011(这是文章所针对的)处理.xaml
不同的代码。我不确定VS2012如何处理它,所以你需要弄清楚那部分。但在自定义 Silverlight 控件背后的代码中,您需要一个构造函数、分别设置this.DialogResult
为OKtrue
和false
Cancel 按钮的函数以及三个属性:
- a
DocumentStream
作为MemoryStream
将文件写入计算机的类型
- a
SaveFileStream
作为FileStream
从数据库中提取文件的类型
- a
FileName
作为String
类型
保存文件的真正工作发生在 Browse 按钮的代码中。
private void BrowseButton_Click(object sender, RoutedEventArgs e) {
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Word Files (*.docx)|*.docx|(*.docm)|*.docm";
saveFileDialog.DefaultExt = "docx";
if ((saveFileDialog.ShowDialog() == true)) {
// Get File name, store and display to user
this.FileTextBox.Text = saveFileDialog.FileName;
this.m_FileName = saveFileDialog.FileName;
this.FileTextBox.IsReadOnly = true;
try {
// Open and store the File Stream
this.DocumentStream = saveFileDialog.OpenFile();
}
catch (IOException ex) {
// Inform the user of the problem
MessageBox.Show("The file you are trying to save to is open in another applicaion." + "\r\n" + "Please close it and try again.", "File Already Open", MessageBoxButton.OK);
}
}
}
我有Filter
andDefaultExt
设置来处理 Word 文档,但你可以修改它来处理.txt
或任何你想要的。根据我的使用经验,我还添加了比文章中的示例更多的错误处理。
最后,在您的屏幕代码中,您需要调用Main
调度程序上的自定义控件,并在自定义控件关闭时将文件写入数据库。
partial void DownloadFile_Execute()
{
// To invoke our own dialog, we have to do this inside of the "Main" Dispatcher
// And, since this is a web application, we can't directly invoke the Silverlight OpenFileDialog
// class, we have to first invoke our own Silverlight custom control (i.e. DownloadFileWindow)
// and that control will be able to invoke the OpenFileDialog class (via the Browse button)
Dispatchers.Main.BeginInvoke(() =>
{
DownloadFileWindow downloadFileWindow = new DownloadFileWindow();
downloadFileWindow.Closed += new EventHandler(downloadFileWindow_Closed);
downloadFileWindow.Show();
});
}
void downloadFileWindow_Closed(object sender, EventArgs e)
{
// Invoked when our custom Silverlight window closes
DownloadFileWindow downloadFile = (DownloadFileWindow)sender;
try {
// Continue if they hit the OK button AND they selected a file
if ((downloadFile.DialogResult == true)) {
// Write the document data stream from the database to the selected file
using (Stream saveStream = new Stream(downloadFile.DocumentStream))
{
downloadFile.DocumentStream.WriteTo(saveStream);
}
}
// Close and release the streams
downloadFile.DocumentStream.Close();
downloadFile.DocumentStream.Dispose();
downloadFile.SaveFileStream.Close();
downloadFile.SaveFileStream.Dispose();
}
catch (Exception ex) {
string res = "One or more save location errors have occured:" + "\r\n";
// Check to see if there is a Document Stream
if ((downloadFile.DocumentStream == null)) {
res = res + '\t' + "Document Stream is empty" + "\r\n";
}
// Check to see if there is a Save File Stream
if ((downloadFile.SaveFileStream == null)) {
res = res + '\t' + "Save File Stream is empty" + "\r\n";
}
// Check to see if there is a Safe File Name
if ((downloadFile.SafeFileName == null)) {
res = res + '\t' + "Safe File Name is empty" + "\r\n";
}
res = res + "\r\n" + "Please use the Browse button to select a location to save to. Specify a file name and then click Save.";
this.ShowMessageBox(res, "Save Location Error", MessageBoxOption.Ok);
}
}
如您所见,根据经验,我还为这些函数添加了更多错误处理。希望在这两篇文章和我的示例之间,您可以创建您想要的功能。
注意:我从 VB.NET 转换了代码,这是我的项目编写的内容。因此,您可能需要更正一些错误,以使 C# 的语法正确。