Articles → LIGHT SWITCH 2011 → Upload And Download File In Lightswitch 2011
Upload And Download File In Lightswitch 2011
Software Requirement
- Visual studio 2010 is installed on your machine.
- Visual Studio 2010 service pack 1 is installed on your machine.
- Microsoft Visual Studio Light switch 2011 is installed on your machine.
Prerequisite Knowledge
- What is lightswitch?
- How to create buttons?
- What is button’s Execute() method?
- What is dispatcher and why we use it?
- How to create lightswitch screens?
- Basics about web applications in .net.
Creation Of File Table
Click to Enlarge
Add A New Editable Grid
Click to Enlarge
Click to Enlarge
Remove Content Field From Grid
Click to Enlarge
Add Upload, Download And Save Data Buttons
Click to Enlarge
Click to Enlarge
Add Silverlight Child Window
Click to Enlarge
Click to Enlarge
Write Xaml Code In Silverlight File
<controls:ChildWindow x:Class="LightSwitchApplication.UploadControl"
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="400" Height="120">
<Grid x:Name="LayoutRoot" Margin="2">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBox Height="30" HorizontalAlignment="Right" Name="FileTextBox" VerticalAlignment="Top" Width="200"
IsHitTestVisible="False" Margin="5" IsTabStop="False" />
<Button Content="Browse" Height="30" HorizontalAlignment="Left" Name="BrowseButton" Margin="5"
VerticalAlignment="Top" Width="75" Click="BrowseButton_Click" Grid.Column="1"/>
<Button x:Name="OKButton" Content="OK" Click="OKButton_Click" Width="75" Height="30" Margin="5" HorizontalAlignment="Right" Grid.Row="1" />
<Button x:Name="CancelButton" Content="Cancel" Click="CancelButton_Click" Width="75" Height="30" Margin="5" HorizontalAlignment="Left" Grid.Row="1" Grid.Column="1" />
</Grid>
</controls:ChildWindow>
Click to Enlarge
Add Properties In Xaml Code Behind
public FileStream Stream { get; set; }
public string FileName { get; set; }
Add Button Click Events In Code Behind In Silverlight
private void OKButton_Click(object sender, RoutedEventArgs e) {
this.DialogResult = true;
}
private void CancelButton_Click(object sender, RoutedEventArgs e) {
this.DialogResult = false;
}
private void BrowseButton_Click(object sender, RoutedEventArgs e) {
OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == true) {
this.FileTextBox.Text = openFileDialog.File.Name;
FileStream myStream = openFileDialog.File.OpenRead();
Stream = myStream;
FileName = System.IO.Path.GetFileName(this.FileTextBox.Text);
}
}
Add Code In Upload Button Click In Lightswitch Project
partial void Upload_Execute() {
Dispatchers.Main.BeginInvoke(() => {
UploadControl ctrl = new UploadControl(); // line 1
ctrl.Closed += new EventHandler(ctrl_Closed); // line 2
ctrl.Show(); // line 3
});
}
Byte[] content;
string fileName;
void ctrl_Closed(object sender, EventArgs e) {
// Write your code here.
UploadControl ctrl = (UploadControl) sender;
Byte[] b;
using(BinaryReader br = new BinaryReader(ctrl.Stream)) {
b = br.ReadBytes(Convert.ToInt32(ctrl.Stream.Length));
}
content = b;
fileName = ctrl.FileName;
}
Add Code On Save Button In Lightswitch Project
partial void SaveData_Execute() {
LightSwitchApplication.File f = this.DataWorkspace.ApplicationData.Files.AddNew();
f.FileContent = content;
f.FileName = fileName;
this.DataWorkspace.ApplicationData.SaveChanges();
}
Add Download.Aspx Webform File In Lightswitch Project
Click to Enlarge
Click to Enlarge
Add Download.Aspx Entry In Project File
Click to Enlarge
Click to Enlarge
Click to Enlarge
Click to Enlarge
Click to Enlarge
Add Dlls In Lightswitch Project
Click to Enlarge
Add Download Button Click Code In Lightswitch Project
partial void Download_Execute() {
// Write your code here.
int fileId = this.Files.SelectedItem.Id;
Dispatchers.Main.Invoke(() => {
HtmlPage.Window.Navigate(new Uri(string.Format("{0}",
String.Format(@ "Download.aspx?id={0}",
fileId)), UriKind.Relative), "_new");
});
}
Connection String In Web.Config File
Click to Enlarge
Add Code In Webform’S Page Load
protected void Page_Load(object sender, EventArgs e) {
if (Request.QueryString["id"] != null) {
int id = Convert.ToInt32(Request.QueryString["id"]);
SqlConnection conn = new SqlConnection();
conn.ConnectionString = ConfigurationManager.ConnectionStrings["_IntrinsicData"].ConnectionString;
conn.Open();
DataSet ds = new DataSet();
SqlDataAdapter adap = new SqlDataAdapter("select * from Files where Id = " + id, conn);
adap.Fill(ds);
if (ds != null) {
HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.ClearContent();
response.ClearHeaders();
response.Buffer = true;
response.AddHeader("Content-Disposition", "attachment;filename=\"" + ds.Tables[0].Rows[0]["FileName"] + "\"");
response.BinaryWrite((byte[]) ds.Tables[0].Rows[0]["FileContent"]);
response.End();
}
conn.Close();
}
}
Output
Click to Enlarge
Click to Enlarge
Click to Enlarge
Click to Enlarge