Skip to main content

Microsoft Silverlight

Answered Question HyperlinkButton not downloading csv text file from server location to clent in browser window.RSS Feed

(0)

hazz
hazz

Member

Member

74 points

258 Posts

HyperlinkButton not downloading csv text file from server location to clent in browser window.

Given the HyperlinkButton xaml, the code behind for the button click, the service level creation of the csv/xls file, why is the browser not launching any "Save as' file dialog box or prompt when I click on the hyperlink?  The .csv file is created in the directory at the root of the web project.  Whether I hard code the actual path in the NavigateUri property to try a relative location as below,... neither works.   Thank you for any help.

 <HyperlinkButton Width="100"
       NavigateUri="../testfile.csv"
       Content="Save To Excel"
       TargetName="_parent"
       Click="Button_Click">
</HyperlinkButton>

 and

private void Button_Click(object sender, RoutedEventArgs e)
{
  if (HtmlPage.Window.Confirm("Save report contents to Excel?"))
  {
     proxy.ExportDataTableToCSVCompleted += new EventHandler (proxy_ExportDataTableToCSVCompleted);
     proxy.ExportDataTableToCSVAsync();

 and

private void exportDataTableToCsv(DataTable formattedDataTable, string filename)
{
  StringBuilder sb = new StringBuilder();
  foreach (DataColumn column in toExcel.Columns)
  {
    sb.Append(column.ColumnName + ",");
  }
  sb.Append(Environment.NewLine);
  foreach (DataRow row in toExcel.Rows)
  {
    for (int i = 0; i < toExcel.Columns.Count; i++)
    {
       sb.Append(rowIdea.ToString().Replace(",", string.Empty) + ",");
    }
    sb.Append(Environment.NewLine);
  }
  WriteFile(sb);
}
and  
void WriteFile(StringBuilder stringCSV)
{
   string FileLocation = "C:\\.....\\.....\\....\\trunk";
   string FileName = "testfile.csv";
   FileStream fileStream = null;
   StreamWriter writer = null;
   fileStream = new FileStream(FileLocation +
                               FileName, FileMode.OpenOrCreate,
                               FileAccess.Write);
    writer = new StreamWriter(fileStream);
    writer.WriteLine(stringCSV.ToString());
    writer.Flush();
}
 
 

 

 

hazz

jay nanavati
jay nana...

Contributor

Contributor

3388 points

624 Posts

Answered Question

Re: HyperlinkButton not downloading csv text file from server location to clent in browser window.

Remove Click event handler and try again.

Jay K Nanavaty
www.technologyopinion.com
Mark as answer if it helps. It will also help others...

hazz
hazz

Member

Member

74 points

258 Posts

Re: HyperlinkButton not downloading csv text file from server location to clent in browser window.

Thank you Jay!  That did it!

Follow up question.

My intention was to have a button on the same page as the report/datagrid which

 1.  calls a service to create the file on the server and then

 2.  navigates to the uri  of that newly created file via ( NavigateUri=http://localhost:1329/xxxx/testfile.csv ) .

 How can I accomplish that if I can't call the click event which calls the service which creates the file AND then navigate to that uri?

I can see I was expecting too much from the Hyperlinkbutton.......but how do I accomplish both steps with one button?

Thank you,

hazz

jay nanavati
jay nana...

Contributor

Contributor

3388 points

624 Posts

Re: Re: HyperlinkButton not downloading csv text file from server location to clent in browser window.

ok hazz, I am preparing sample for you and will post here.

Jay K Nanavaty
www.technologyopinion.com
Mark as answer if it helps. It will also help others...

jay nanavati
jay nana...

Contributor

Contributor

3388 points

624 Posts

Re: Re: Re: HyperlinkButton not downloading csv text file from server location to clent in browser window.

Hey, download the example from here:

http://cid-b55690d11b67401d.skydrive.live.com/embedrow.aspx/Public/FileCreate.zip

Jay K Nanavaty
www.technologyopinion.com
Mark as answer if it helps. It will also help others...

hazz
hazz

Member

Member

74 points

258 Posts

Re: Re: Re: HyperlinkButton not downloading csv text file from server location to clent in browser window.

Sweet.

  1. Use a button, not a hyperlink (as I was doing with the Hyperlinkbutton) -  a button is truly the only answer here.
  2. Stashing the newly created file in it's own folder ('FileDir') off the web project folder.. Mulitple reports (separate silverlight projects) can have their own FileDir off the web project folder.
  3. Returning this asynchronously into it's newly opened browser window via 'HtmlPage.Window.Navigate' after the server has done it's business. 

Very nice. 

With much appreciation. 

 Big Smile

 

hazz
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities