我想在我的应用程序中创建一个简单的文本文件,并允许用户下载它。我有一个使用lightswitch
构建的Web应用程序。我已经谷歌了,而且响应并没有帮助我,因为它与lightswitch有关。
Download text as file in ASP.NET
此处,建议使用Response。但是,在lightswitch中我无法使用该特定命名空间。我尝试使用webclient,但我拒绝访问。我的代码如下。
using (System.Net.WebClient wc = new System.Net.WebClient()) {
string path = AppDomain.CurrentDomain.GetData("DataDirectory").ToString() + "\\" + Application.Current.User.FullName + ".txt";
string text = "A class is the most powerful data type in C#. Like a structure, " + "a class defines the data and behavior of the data type. ";
System.IO.File.WriteAllText(path, text);
wc.DownloadFile(AppDomain.CurrentDomain.GetData("DataDirectory").ToString(), Application.Current.User.FullName + ".txt");
}
我可以将我的文本文件写入APP_DATA
。但是,当我尝试使用webclient
下载它时,它会让我拒绝访问。请帮帮我,谢谢。
答案 0 :(得分:1)
我在网络应用程序中上传和下载Word文件。我跟着两篇文章来帮助我实现这个目标:
How Do I: Import and Store a Data File(这个只讨论上传,但过程类似) Upload and download file in 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控件背后的代码中,您需要一个构造函数,分别为OK和Cancel按钮设置this.DialogResult
到true
和false
的函数和三个属性:
DocumentStream
作为MemoryStream
类型将文件写入您的计算机SaveFileStream
作为FileStream
类型从数据库中提取文件FileName
作为String
类型保存文件的实际工作发生在“浏览”按钮的代码中。
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
和DefaultExt
来处理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#的语法正确。