silverlight导入文件到WebService
SL端代码
private void btnSelectFile_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openFile = new OpenFileDialog();
openFile.Filter = "Excel文件|*.xls";
if (openFile.ShowDialog() == true)
{
if (HtmlPage.Window.Confirm(string.Format("确定要导入“{0}”吗?", openFile.File.Name)))
{
KeywordServices.KeywordServicesSoapClient client = SLHelpers.SoapClientFactory.CreateKeywordServicesSoapClient();
client.ImportExcelCompleted += new EventHandler<KeywordServices.ImportExcelCompletedEventArgs>(client_ImportExcelCompleted);
Stream stream = (Stream)openFile.File.OpenRead();
stream.Position = 0;
byte[] buffer = new byte[stream.Length + 1];
stream.Read(buffer, 0, buffer.Length);
client.ImportExcelAsync(buffer);
}
}
}
{
OpenFileDialog openFile = new OpenFileDialog();
openFile.Filter = "Excel文件|*.xls";
if (openFile.ShowDialog() == true)
{
if (HtmlPage.Window.Confirm(string.Format("确定要导入“{0}”吗?", openFile.File.Name)))
{
KeywordServices.KeywordServicesSoapClient client = SLHelpers.SoapClientFactory.CreateKeywordServicesSoapClient();
client.ImportExcelCompleted += new EventHandler<KeywordServices.ImportExcelCompletedEventArgs>(client_ImportExcelCompleted);
Stream stream = (Stream)openFile.File.OpenRead();
stream.Position = 0;
byte[] buffer = new byte[stream.Length + 1];
stream.Read(buffer, 0, buffer.Length);
client.ImportExcelAsync(buffer);
}
}
}
WebService端代码
public bool ImportExcel(byte[] fileByte)
{
try
{
string filePath = "c:\\a.xls";
FileStream stream = new FileStream(filePath, FileMode.Create);
stream.Write(fileByte, 0, fileByte.Length);
stream.Close();
return true;
}
catch (Exception ex)
{
//TODO:写日志
return false;
}
}
{
try
{
string filePath = "c:\\a.xls";
FileStream stream = new FileStream(filePath, FileMode.Create);
stream.Write(fileByte, 0, fileByte.Length);
stream.Close();
return true;
}
catch (Exception ex)
{
//TODO:写日志
return false;
}
}