读书人

silverlight中HttpResponse,该如何解决

发布时间: 2012-12-26 14:39:29 作者: rapoo

silverlight中HttpResponse
我要写一个单击按钮,提示一个提示框,上面有保存和打开,代码如下

HttpResponse Response = HttpContext.Current.Response;

Response.ContentType = "application/soap+msbin1";
//通知浏览器下载文件
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode("Excel文件", System.Text.Encoding.UTF8));
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();


为什么不对啊,老是提示错误,求指点
[解决办法]
Silverlight是客户端的,不是服务器端的,没有Response.AddHeader

Response.AddHeader需要写到服务器端的asp.net中
[解决办法]
不明白你要干什么
你可以使用SaveFileDialog
http://www.silverlightshow.net/items/Using-the-SaveFileDialog-in-Silverlight-3.aspx


代码

<UserControl x:Class="FileSaveDialogDemo.MainPage" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
<Canvas x:Name="LayoutRoot" Background="White">
<Button x:Name="btnSaveFile" Width="100" Height="20"
Content="Save File" Click="btnSaveFile_Click"
Canvas.Top="10" Canvas.Left="10"></Button>
<TextBlock x:Name="tblError" Canvas.Top="40" Canvas.Left="10"></TextBlock>
</Canvas>
</UserControl>


using System;  
using System.IO;
using System.Windows;
using System.Windows.Controls;
using FileSaveDialogDemo.FilesServiceReference;

namespace FileSaveDialogDemo
{
public partial class MainPage : UserControl
{
#region Fields
private SaveFileDialog dialog;
#endregion

#region Constructors
public MainPage()
{
InitializeComponent();

this.dialog = new SaveFileDialog();



try
{
this.dialog.DefaultExt = ".txt";
this.dialog.Filter = "Text Files
[解决办法]
*.txt
[解决办法]
Log Files
[解决办法]
*.log
[解决办法]
All Files
[解决办法]
*.*";
this.dialog.FilterIndex = 2;
}
catch ( Exception ex )
{
this.tblError.Text = "Error configuring SaveFileDialog: " + ex.Message;
}
}
#endregion

#region Handlers
private void btnSaveFile_Click( object sender, RoutedEventArgs e )
{
bool? dialogResult = this.dialog.ShowDialog();

if ( dialogResult == true )
{
try
{
FilesServiceReference.FilesClient fileClient
= new FilesClient();
fileClient.GetFileCompleted
+= new EventHandler<GetFileCompletedEventArgs>(
fileClient_GetFileCompleted );
fileClient.GetFileAsync();



this.tblError.Text = "Getting file from the server...";
}
catch ( Exception ex )
{
this.tblError.Text = "Error calling service: " + ex.Message;
}
}
}
void fileClient_GetFileCompleted( object sender, GetFileCompletedEventArgs e )
{
try
{
byte[] fileBytes = e.Result as byte[];

using ( Stream fs = ( Stream )this.dialog.OpenFile() )
{
fs.Write( fileBytes, 0, fileBytes.Length );
fs.Close();

this.tblError.Text = "File successfully saved!";
}
}
catch ( Exception ex )
{
this.tblError.Text = "Error getting result: " + ex.Message;
}
}
#endregion

}
}




读书人网 >.NET Framework

热点推荐